Comparing version 0.1.4 to 0.1.5
153
bugsnag.js
@@ -1,5 +0,4 @@ | ||
//TODO: The set user id and set context functions wont be scoped to each request in express.js for example. | ||
trace = require('tracejs').trace; | ||
http = require('http'); | ||
https = require('https') | ||
fs = require('fs'); | ||
@@ -17,6 +16,8 @@ path = require('path'); | ||
var releaseStage; | ||
var userIdLambda; | ||
var contextLambda; | ||
var projectDirectory; | ||
var applicationVersion = "unknown"; | ||
var appVersion; | ||
var notifyReleaseStages; | ||
var autoNotify; | ||
var enableSSL; | ||
var onUncaughtException = function(err) { | ||
@@ -27,2 +28,37 @@ console.log(err); | ||
// Set a lambda function to detail what happens once an uncaught exception is processed. Defaults to exit(1) | ||
exports.setUncaughtExceptionHandler = function(lambda) { | ||
onUncaughtException = lambda; | ||
} | ||
// Register to process uncaught exceptions properly | ||
exports.register = function(apiKey, options) { | ||
options = (options === undefined ? {} : options) | ||
Error.stackTraceLimit = Infinity; | ||
defaultErrorHash.apiKey = apiKey; | ||
releaseStage = (options.releaseStage === undefined ? "production" : options.releaseStage); | ||
appVersion = (options.appVersion === undefined ? undefined : options.appVersion); | ||
autoNotify = (options.autoNotify === undefined ? true : options.autoNotify); | ||
notifyReleaseStages = (options.notifyReleaseStages === undefined ? ["production"] : options.notifyReleaseStages); | ||
enableSSL = (options.enableSSL === undefined ? undefined : options.enableSSL); | ||
if ( options.packageJSON !== undefined ) { | ||
appVersion = getPackageVersion(options.packageJSON); | ||
} | ||
if( appVersion === undefined || appVersion == "unknown" ) { | ||
appVersion = getPackageVersion(path.join(__dirname, '../../package.json')); | ||
} | ||
projectDirectory = (options.projectDirectory === undefined ? path.join(__dirname, "../..") : options.projectDirectory); | ||
defaultErrorHash.notifier.version = getPackageVersion(path.join(__dirname,'package.json')); | ||
if(autoNotify) { | ||
process.on('uncaughtException', function(err) { | ||
exports.notify(err); | ||
onUncaughtException(err); | ||
}); | ||
} | ||
} | ||
getPackageVersion = function(packageJSON) { | ||
@@ -38,14 +74,20 @@ try { | ||
exports.handleExceptions = function() { | ||
process.on('uncaughtException', function(err) { | ||
exports.notify(err); | ||
onUncaughtException(err); | ||
}); | ||
} | ||
// Send a test notification | ||
exports.testNotification = function() { | ||
exports.notify(new Error("Test error ")); | ||
exports.notify(new Error("Test error")); | ||
} | ||
exports.notifyWithClass = function(errorClass, error) { | ||
var errorMessage; | ||
if(typeof error == 'string') { | ||
errorMessage = error; | ||
error = new Error(error); | ||
} else { | ||
errorMessage = stacktrace.first_line.split(": ")[1] | ||
} | ||
var stacktrace = trace(error); | ||
notifyError(errorClass, errorMessage, stacktrace); | ||
} | ||
// Notify about a caught error | ||
@@ -56,9 +98,17 @@ exports.notify = function(error) { | ||
return; | ||
} else { | ||
console.log("Bugnsag: Notifying bugsnag of error"); | ||
} | ||
if(typeof error == 'string') { | ||
error = new Error(error); | ||
} | ||
var stacktrace = trace(error); | ||
errorClass = stacktrace.first_line.split(": ")[0]; | ||
errorMessage = stacktrace.first_line.split(": ")[1]; | ||
notifyError(errorClass, errorMessage, stacktrace); | ||
} | ||
notifyError = function(errorClass, errorMessage, stacktrace) { | ||
var errorList = [{ | ||
userId: userIdLambda(), | ||
appVersion: applicationVersion, | ||
appVersion: appVersion, | ||
releaseStage: releaseStage, | ||
@@ -69,8 +119,8 @@ metaData: { | ||
exceptions: [{ | ||
errorClass: stacktrace.first_line.split(": ")[0], | ||
message: stacktrace.first_line.split(": ")[1], | ||
errorClass: errorClass, | ||
message: errorMessage, | ||
stacktrace: [] | ||
}] | ||
}]; | ||
errorList[0].metadata.environment.memoryUsage = process.memoryUsage(); | ||
errorList[0].metaData.environment.memoryUsage = JSON.stringify(process.memoryUsage()); | ||
@@ -91,14 +141,11 @@ for(var i = 0, len = stacktrace.frames.length; i < len; ++i) { | ||
} | ||
if ( contextLambda !== undefined ) { | ||
errorList[0].context = contextLambda(); | ||
} | ||
defaultErrorHash.errors = errorList; | ||
var payload = JSON.stringify(defaultErrorHash); | ||
var port = enableSSL ? 443 : 80; | ||
var options = { | ||
host: 'api.bugsnag.com', | ||
port: 8000, | ||
path: '/notify', | ||
host: 'notify.bugsnag.com', | ||
port: port, | ||
path: '/', | ||
method: 'POST', | ||
@@ -110,47 +157,11 @@ headers: { | ||
}; | ||
var req = http.request(options, function(response) {}); | ||
var req; | ||
if(enableSSL) { | ||
req = https.request(options, function(response) {}); | ||
} else { | ||
req = http.request(options, function(response) {}); | ||
} | ||
req.write(payload, 'utf8'); | ||
req.end(); | ||
} | ||
// Register to process uncaught exceptions properly | ||
exports.register = function(apiKey, options) { | ||
options = (options === undefined ? {} : options) | ||
Error.stackTraceLimit = Infinity; | ||
defaultErrorHash.apiKey = apiKey; | ||
releaseStage = (options.releaseStage === undefined ? "release" : options.releaseStage); | ||
contextLambda = (options.context === undefined ? undefined : options.context); | ||
userIdLambda = (options.userId === undefined ? function() {return "unknown";} : options.userId); | ||
applicationVersion = (options.applicationVersion === undefined ? undefined : options.applicationVersion); | ||
if ( options.packageJSON !== undefined ) { | ||
applicationVersion = getPackageVersion(options.packageJSON); | ||
} | ||
if( applicationVersion === undefined || applicationVersion == "unknown" ) { | ||
applicationVersion = getPackageVersion(path.join(__dirname, '../../package.json')); | ||
} | ||
projectDirectory = (options.projectDirectory === undefined ? path.join(__dirname, "../..") : options.projectDirectory); | ||
defaultErrorHash.notifier.version = getPackageVersion(path.join(__dirname,'package.json')) | ||
} | ||
// Set a lambda function to detail the context of the call | ||
exports.setContext = function(lambda) { | ||
contextLambda = lambda | ||
} | ||
// Set a lambda function to detail the user affected | ||
exports.setUserId = function(lambda) { | ||
userIdLambda = lambda; | ||
} | ||
// Set a lambda function to detail what happens once an uncaught exception is processed. Defaults to exit(1) | ||
exports.setUncaughtExceptionHandler = function(lambda) { | ||
onUncaughtException = lambda; | ||
} | ||
// Set the application version | ||
exports.setApplicationVersion = function(version) { | ||
applicationVersion = version; | ||
} |
{ | ||
"name": "bugsnag", | ||
"description": "Bugsnag notifier for node.js scripts", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"main": "./bugsnag.js", | ||
@@ -6,0 +6,0 @@ "homepage": "http://bugsnag.com", |
# Bugsnag Node Plugin | ||
Node.js client for [bugsnag](http://www.bugsnag.com). | ||
## Install | ||
``` bash | ||
npm install bugsnag | ||
``` | ||
## Usage | ||
Typically you will register for all uncaught exceptions to be sent to bugsnag. The following code will send all uncaught exceptions to bugsnag. | ||
``` javascript | ||
var bugsnag = require('bugsnag').register("your api key"); | ||
bugsnag.handleExceptions(); | ||
throw new Error('Uncaught exception'); | ||
``` | ||
You can also send an individual error to bugsnag in the following manner. | ||
``` javascript | ||
var bugsnag = require('bugsnag').register("your api key"); | ||
bugsnag.handleExceptions(); | ||
try { | ||
throw new Error('Uncaught exception'); | ||
} catch(e) { | ||
bugsnag.notify(e); | ||
} | ||
``` | ||
## Advanced Usage | ||
### Application Version | ||
The application version is derived from the package.json associated with your project. If that isn't | ||
sufficient for your needs, it can be set manually using the following API call. | ||
``` javascript | ||
bugsnag.setApplicationVersion("1.1.1"); | ||
``` | ||
### Uncaught Exception Handler | ||
When bugsnag catches an uncaught exception, by default it logs the exception and exits the process. If | ||
you would prefer different functionality, you can change this behavior by passing a function to bugsnag. | ||
This function will be run after bugsnag has notified bugsnag.com and the exception has been processed. | ||
``` javascript | ||
bugsnag.setUncaughtExceptionHandler(function(){ | ||
exit(1); | ||
}); | ||
``` | ||
The Node client is currently in development. |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
139
5044
3