Comparing version 0.1.5 to 0.1.6
137
bugsnag.js
@@ -21,2 +21,5 @@ trace = require('tracejs').trace; | ||
var enableSSL; | ||
var userId; | ||
var context; | ||
var extraData; | ||
@@ -28,2 +31,14 @@ var onUncaughtException = function(err) { | ||
exports.setUserId = function(passedUserId) { | ||
userId = passedUserId; | ||
} | ||
exports.setContext = function(passedContext) { | ||
context = passedContext; | ||
} | ||
exports.setExtraData = function(passedExtraData) { | ||
extraData = passedExtraData; | ||
} | ||
// Set a lambda function to detail what happens once an uncaught exception is processed. Defaults to exit(1) | ||
@@ -58,6 +73,8 @@ exports.setUncaughtExceptionHandler = function(lambda) { | ||
process.on('uncaughtException', function(err) { | ||
exports.notify(err); | ||
exports.notify(err, {userId: userId, context: context}); | ||
onUncaughtException(err); | ||
}); | ||
} | ||
return exports.handle | ||
} | ||
@@ -75,3 +92,68 @@ | ||
// Send a test notification | ||
getUserIdFromOptions = function(options) { | ||
var localUserId = null; | ||
if(options.userId !== undefined && options.userId != null) { | ||
localUserId = options.userId | ||
} else if(options.req !== undefined && options.req != null){ | ||
localUserId = options.req.connection.remoteAddress; | ||
if(localUserId === undefined || localUserId == null || localUserId == "127.0.0.1") { | ||
try { | ||
var temp = options.req.headers['x-forwarded-for']; | ||
if(temp !== undefined && temp != null) { | ||
localUserId = temp; | ||
} | ||
}catch(e){} | ||
} | ||
} | ||
if(localUserId === undefined || localUserId == null) { | ||
localUserId = userId; | ||
} | ||
return localUserId; | ||
} | ||
getContextFromOptions = function(options) { | ||
var localContext = null; | ||
if(options.context !== undefined && options.context != null) { | ||
localContext = options.context; | ||
} else if(options.req !== undefined && options.req != null) { | ||
localContext = options.req.url; | ||
} else { | ||
localContext = context; | ||
} | ||
return localContext; | ||
} | ||
getExtraDataFromOptions = function(options) { | ||
var localExtraData = null; | ||
if(options.extraData !== undefined && options.extraData != null) { | ||
localExtraData = options.extraData; | ||
} else if(extraData !== undefined && extraData != null) { | ||
localExtraData = extraData; | ||
} else { | ||
localExtraData = {}; | ||
} | ||
if(options.req !== undefined && options.req != null) { | ||
var requestHash = {}; | ||
requestHash["url"] = options.req.url; | ||
requestHash["method"] = options.req.method; | ||
requestHash["headers"] = options.req.headers; | ||
requestHash["httpVersion"] = options.req.httpVersion; | ||
var connectionHash = {} | ||
connectionHash["remoteAddress"] = options.req.connection.remoteAddress; | ||
connectionHash["remotePort"] = options.req.connection.remotePort; | ||
connectionHash["bytesRead"] = options.req.connection.bytesRead; | ||
connectionHash["bytesWritten"] = options.req.connection.bytesWritten; | ||
connectionHash["localPort"] = options.req.connection.address()["port"]; | ||
connectionHash["localAddress"] = options.req.connection.address()["address"]; | ||
connectionHash["IPVersion"] = options.req.connection.address()["family"]; | ||
requestHash["connection"] = connectionHash; | ||
extraData["request"] = requestHash; | ||
} | ||
return extraData; | ||
} | ||
// Send a test error | ||
exports.testNotification = function() { | ||
@@ -81,3 +163,12 @@ exports.notify(new Error("Test error")); | ||
exports.notifyWithClass = function(errorClass, error) { | ||
// Handle connect errors (also express) | ||
exports.handle = function(err, req, res, next) { | ||
exports.notify(err, {req:req}); | ||
next(err); | ||
} | ||
// Notify with a specified error class | ||
exports.notifyWithClass = function(errorClass, error, options) { | ||
options = (options === undefined ? {} : options) | ||
var errorMessage; | ||
@@ -92,15 +183,13 @@ if(typeof error == 'string') { | ||
var stacktrace = trace(error); | ||
notifyError(errorClass, errorMessage, stacktrace); | ||
notifyError(errorClass, errorMessage, stacktrace, getUserIdFromOptions(options), getContextFromOptions(options), getExtraDataFromOptions(options)); | ||
} | ||
// Notify about a caught error | ||
exports.notify = function(error) { | ||
if(defaultErrorHash.apiKey == "") { | ||
console.log("Bugsnag: No apiKey set - not notifying."); | ||
return; | ||
} | ||
exports.notify = function(error, options) { | ||
options = (options === undefined ? {} : options) | ||
if(typeof error == 'string') { | ||
error = new Error(error); | ||
} | ||
var stacktrace = trace(error); | ||
@@ -110,12 +199,16 @@ errorClass = stacktrace.first_line.split(": ")[0]; | ||
notifyError(errorClass, errorMessage, stacktrace); | ||
notifyError(errorClass, errorMessage, stacktrace, getUserIdFromOptions(options), getContextFromOptions(options), getExtraDataFromOptions(options)); | ||
} | ||
notifyError = function(errorClass, errorMessage, stacktrace) { | ||
notifyError = function(errorClass, errorMessage, stacktrace, passedUserId, passedContext, extraData) { | ||
if(defaultErrorHash.apiKey == "") { | ||
console.log("Bugsnag: No apiKey set - not notifying."); | ||
return; | ||
} | ||
extraData = (extraData === undefined ? {} : extraData) | ||
var errorList = [{ | ||
appVersion: appVersion, | ||
releaseStage: releaseStage, | ||
metaData: { | ||
environment: process.env | ||
}, | ||
metaData: extraData, | ||
exceptions: [{ | ||
@@ -127,3 +220,13 @@ errorClass: errorClass, | ||
}]; | ||
errorList[0].metaData.environment.memoryUsage = JSON.stringify(process.memoryUsage()); | ||
if(passedUserId !== undefined && passedUserId != null ) { | ||
errorList[0].userId = passedUserId; | ||
} | ||
if(passedContext !== undefined && passedContext != null ) { | ||
errorList[0].context = passedContext; | ||
} | ||
var memUsage = process.memoryUsage(); | ||
errorList[0].metaData.environment = {}; | ||
errorList[0].metaData.environment.memoryUsage = { total: memUsage.heapTotal, used: memUsage.heapUsed }; | ||
@@ -146,4 +249,4 @@ for(var i = 0, len = stacktrace.frames.length; i < len; ++i) { | ||
defaultErrorHash.errors = errorList; | ||
var payload = JSON.stringify(defaultErrorHash); | ||
var payload = JSON.stringify(defaultErrorHash); | ||
var port = enableSSL ? 443 : 80; | ||
@@ -150,0 +253,0 @@ var options = { |
{ | ||
"name": "bugsnag", | ||
"description": "Bugsnag notifier for node.js scripts", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "./bugsnag.js", | ||
@@ -6,0 +6,0 @@ "homepage": "http://bugsnag.com", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
8544
226
1