Comparing version 3.3.4 to 4.0.1
112
error.js
@@ -1,56 +0,84 @@ | ||
var extend = require("xtend") | ||
var sendJson = require("./json") | ||
var isSendObject = require("./is-send-object.js") | ||
'use strict'; | ||
module.exports = sendError | ||
var url = require('url'); | ||
var assert = require('assert'); | ||
var STATUS_CODES = require('http').STATUS_CODES; | ||
var sendJson = require('./json.js'); | ||
module.exports = sendError; | ||
function sendError(req, res, opts, callback) { | ||
if (!isSendObject(opts)) { | ||
opts = { body: opts, statusCode: 500 } | ||
} | ||
assert(opts && opts.body, 'opts.body is required'); | ||
var error = opts.body | ||
var err = opts.body; | ||
var logger = opts.logger; | ||
var statsd = opts.statsd; | ||
if (Array.isArray(error)) { | ||
opts.body = { errors: error } | ||
} else if (typeof error === "string") { | ||
opts.body = { errors: [{ message: error, attribute: "general" }] } | ||
} else if (error && typeof error.message === "string") { | ||
Object.defineProperty(error, "message", { | ||
value: error.message, | ||
enumerable: true, | ||
writable: true, | ||
configurable: true | ||
}) | ||
Object.defineProperty(error, "type", { | ||
value: error.type, | ||
enumerable: true, | ||
writable: true, | ||
configurable: true | ||
}) | ||
var errOpts = { | ||
verbose: opts.verbose || false, | ||
bodyStatusCode: opts.bodyStatusCode, | ||
additionalParams: opts.additionalParams, | ||
err: err | ||
}; | ||
if (!error.attribute) { | ||
error.attribute = "general" | ||
} | ||
var statsPrefix = opts.statsPrefix || 'clients.send-data'; | ||
var parsedUrl = url.parse(req.url); | ||
var statsdKey = statsPrefix + '.error-handler.' + | ||
parsedUrl.pathname; | ||
var serializeError = extend(error) | ||
var isExpected = err.expected || | ||
(err.statusCode >= 400 && err.statusCode <= 499); | ||
if (serializeError.domain && | ||
typeof serializeError.domain.on === "function" | ||
) { | ||
delete serializeError.domain | ||
delete serializeError.domainThrown | ||
if (!isExpected) { | ||
if (logger) { | ||
logger.error('unexpected error', err); | ||
} | ||
if (serializeError.domainEmitter && | ||
serializeError.domainEmitter.domain && | ||
typeof serializeError.domainEmitter.domain.on === "function" | ||
) { | ||
delete serializeError.domainEmitter | ||
if (statsd) { | ||
statsd.increment(statsdKey + '.unexpected'); | ||
} | ||
} else if (statsd) { | ||
statsd.increment(statsdKey + '.expected'); | ||
} | ||
writeError(req, res, errOpts, callback); | ||
} | ||
opts.body = { errors: [serializeError] } | ||
function writeError(req, res, opts, callback) { | ||
var err = opts.err; | ||
var statusCode = err.statusCode || 500; | ||
var body = { | ||
message: err.message || STATUS_CODES[statusCode] || | ||
STATUS_CODES[500] | ||
}; | ||
if (typeof err.type === 'string') { | ||
body.type = err.type; | ||
} | ||
sendJson(req, res, opts, callback) | ||
if (Array.isArray(err.messages)) { | ||
body.messages = err.messages; | ||
} | ||
// Toggle sending status code in the body | ||
if (opts.bodyStatusCode !== false) { | ||
body.statusCode = statusCode; | ||
} | ||
if (opts.verbose) { | ||
body.stack = err.stack; | ||
body.expected = err.expected; | ||
body.debug = err.debug; | ||
} | ||
// Append additional params | ||
if (opts.additionalParams) { | ||
opts.additionalParams.forEach(function appendKey(k) { | ||
body[k] = err[k]; | ||
}); | ||
} | ||
sendJson(req, res, { | ||
statusCode: statusCode, | ||
body: body | ||
}, callback); | ||
} |
{ | ||
"name": "send-data", | ||
"version": "3.3.4", | ||
"version": "4.0.1", | ||
"description": "send data through response", | ||
@@ -24,5 +24,9 @@ "keywords": [], | ||
"devDependencies": { | ||
"test-server": "~0.1.1", | ||
"tape": "~1.0.4", | ||
"tern": "git://github.com/marijnh/tern" | ||
"hammock": "^0.1.10", | ||
"hammock-request": "^2.1.0", | ||
"istanbul": "^0.3.5", | ||
"opn": "^1.0.1", | ||
"tape": "^3.4.0", | ||
"tern": "git://github.com/marijnh/tern", | ||
"test-server": "~0.1.1" | ||
}, | ||
@@ -36,4 +40,6 @@ "licenses": [ | ||
"scripts": { | ||
"test": "node ./test/integration.js" | ||
"test": "node ./test/index.js", | ||
"cover": "istanbul cover --print detail --report html -- test/index.js", | ||
"view-cover": "opn coverage/index.html" | ||
} | ||
} |
@@ -47,3 +47,3 @@ # send-data [![build status][1]][2] | ||
} else if (req.url === "/oops") { | ||
sendError(req, res, new Error("OOPS")) | ||
sendError(req, res, { body: new Error("OOPS") }) | ||
} | ||
@@ -50,0 +50,0 @@ }).listen(8080) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25202
18
664
7
2