Skip to content

Commit

Permalink
tighten up HttpError; 0.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Apr 4, 2021
1 parent 96dda32 commit 56f6b6f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ timestamp and "microrest:".

Construct a `new Error` with additional properties `statusCode`, `debug` and
`details`. The error `.message` is looked up from the status code, eg `404 Not Found`
or `777 Internal Error`. If `statusCode` is an object, all its own properties will be
or `456 Http Error`. If `statusCode` is an object, all its own properties will be
transferred onto the error. `statusCode` will be set in any case, either to the
passed-in number else to its `.statusCode` object property else to `500`.

Expand Down
15 changes: 5 additions & 10 deletions mw.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@ http.IncomingMessage.prototype = toStruct(http.IncomingMessage.prototype);
var nodeVersion = process.version.slice(1, process.version.indexOf('.'));
var nextTick = eval('nodeVersion >= 4 ? process.nextTick : global.setImmediate || process.nextTick');

function HttpError( statusCode, debugMessage, details ) {
var httpCode = Number(statusCode) || (statusCode && statusCode.statusCode);
var err = new Error((httpCode || 500) + ' ' + (http.STATUS_CODES[httpCode] || 'Internal Error'));
err.statusCode = httpCode || 500;
if (statusCode && typeof statusCode !== 'number') {
Object.keys(statusCode).forEach(function(k) { err[k] = statusCode[k] });
}
err.debug = debugMessage;
err.details = details;
return err;
function HttpError( statusCode, message, details ) {
var httpCode = Number(statusCode) || (statusCode && statusCode.statusCode) || 500;
var errorName = (http.STATUS_CODES[httpCode] || (httpCode >= 500 ? 'Internal Error' : 'Http Error'));
return Object.assign(new Error(httpCode + ' ' + errorName + (message > '' ? ': ' + message : '')),
statusCode ? statusCode : {}, { statusCode: httpCode, debug: message}, details !== undefined ? { details: details } : {});
}

// print a warning to stderr
Expand Down
19 changes: 15 additions & 4 deletions test-mw.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ module.exports = {
'should encode statusCode, message, debug': function(t) {
var err = new mw.HttpError(404, 'my error message');
t.equal(err.statusCode, 404);
t.equal(err.message, '404 Not Found');
t.contains(err.message, '404 Not Found');
t.equal(err.debug, 'my error message');

// Http Error
var err = new mw.HttpError(456, 'my message');
t.equal(err.statusCode, 456);
t.equal(err.message, '456 Http Error: my message');

// Internal Error
var err = new mw.HttpError(567, 'oops message');
t.equal(err.statusCode, 567);
t.equal(err.message, '567 Internal Error: oops message');

// without params
var err = new mw.HttpError();
t.equal(err.statusCode, 500);
t.equal(err.message, '500 Internal Error');
t.equal(err.message, '500 Internal Server Error');

// with just a status code
var err = new mw.HttpError(401);
Expand All @@ -59,7 +69,7 @@ module.exports = {
t.equal(err.statusCode, 789);
t.equal(err.debug, 'my message');
t.equal(err.details, 'mock error');
t.equal(err.message, '789 Internal Error');
t.equal(err.message, '789 Internal Error: my message');

// with an empty object statusCode
var err = new mw.HttpError({}, 'my error message');
Expand Down Expand Up @@ -304,7 +314,8 @@ module.exports = {
var spyEnd = t.spyOnce(this.res, 'end');
mw.sendResponse(this.req, this.res, noop, new mw.HttpError(404, 'my page not found', 'check again'));
t.equal(this.res.statusCode, 404);
t.deepEqual(JSON.parse(spyEnd.args[0]), { error: 404, message: '404 Not Found', debug: 'my page not found', details: 'check again' });
t.deepEqual(JSON.parse(spyEnd.args[0]),
{ error: 404, message: '404 Not Found: my page not found', debug: 'my page not found', details: 'check again' });

t.done();
},
Expand Down
2 changes: 1 addition & 1 deletion test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ module.exports = {
this.router.runRoute({}, this.req, {}, function(err) {
t.ok(err);
t.equal(err.statusCode, 404);
t.equal(err.message, '404 Not Found');
t.contains(err.message, '404 Not Found');
t.done();
})
},
Expand Down

0 comments on commit 56f6b6f

Please sign in to comment.