Skip to content

Commit

Permalink
Upgrade for Express 4
Browse files Browse the repository at this point in the history
  • Loading branch information
javascript-journal committed Oct 18, 2014
1 parent 6003d48 commit 75cbe26
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 53 deletions.
26 changes: 20 additions & 6 deletions error-handler.js
Expand Up @@ -19,6 +19,7 @@ var mixIn = require('mout/object/mixIn'),
path = require('path'),
fs = require('fs'),
statusCodes = require('http').STATUS_CODES,
stringify = require('json-stringify-safe'),

/**
* Return true if the error status represents
Expand Down Expand Up @@ -94,17 +95,18 @@ var mixIn = require('mout/object/mixIn'),
},

send = function send(statusCode, err, res, o) {
var body = mixIn({}, {
var body = {
status: statusCode,
message: err.message ||
statusCodes[statusCode]
});
};

body = (o.serializer) ?
o.serializer(body) :
body;

res.send(statusCode, body);
res.statusCode = statusCode;
res.send(body);
},

defaults = {
Expand Down Expand Up @@ -228,13 +230,25 @@ createHandler = function createHandler(options) {
if (express) {
return res.format({
json: function () {
send(statusCode, err, res, o);
send(statusCode, err, res, {
serializer: o.serializer || function (o) {
return o;
}
});
},
text: function () {
res.send(statusCode);
send(statusCode, err, res, {
serializer: function (o) {
return o.message;
}
});
},
html: function () {
res.send(statusCode);
send(statusCode, err, res, {
serializer: function (o) {
return o.message;
}
});
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/app.js
Expand Up @@ -30,7 +30,7 @@ app.get('/log', noCache,

// Since all requests are automatically logged,
// all you need to do is send the response:
res.send(200);
res.status(200).end();
});

// Route that triggers a sample error:
Expand Down
19 changes: 10 additions & 9 deletions package.json
Expand Up @@ -24,18 +24,19 @@
"url": "https://github.com/dilvie/express-error-handler/issues"
},
"dependencies": {
"mout": "~0.7.1",
"connect-domain": "~0.5.0"
"connect-domain": "~0.5.0",
"json-stringify-safe": "^5.0.0",
"mout": "~0.7.1"
},
"devDependencies": {
"express": "~3.4.0",
"tape": "~1.1.1",
"supertest": "~0.8.0",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.4",
"bunyan-request-logger": "~0.1.1",
"connect-cache-control": "~0.1.0",
"through": "~2.3.4",
"restify": "~2.6.0"
"express": "^4.9.8",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.4",
"restify": "~2.6.0",
"supertest": "~0.8.0",
"tape": "~1.1.1",
"through": "~2.3.4"
}
}
81 changes: 44 additions & 37 deletions test/runtests.js
Expand Up @@ -3,18 +3,24 @@
var test = require('tape'),
createHandler = require('../error-handler.js'),
through = require('through'),
mixIn = require('mout/object/mixIn'),

format = function format (types) {
return types['text']();
},

testError = new Error('Test error'),
testReq = function () { return {}; },
testRes = function () {
return {
testRes = function (config) {
return mixIn({
send: function send() {},
format: format
};
end: function end() {},
format: format,
status: function (statusCode) {
this.statusCode = statusCode;
return this;
}
}, config);
},
testNext = function () {};

Expand Down Expand Up @@ -101,17 +107,18 @@ test('Custom views', function (t) {
views: {
'404': '404 view'
}
});

e.status = 404;

handler(e, testReq(), {
render: function render() {
}),
res = testRes({
render: function render() {
t.pass('Render should be called for ' +
'custom views.');
t.end();
}
},testNext);
});

e.status = 404;

handler(e, testReq(), res, testNext);
});

test('Error with status default behavior',
Expand All @@ -121,21 +128,22 @@ test('Error with status default behavior',
t.fail('shutdown should not be called.');
},
e = new Error(),
status = 404,
handler = createHandler({
shutdown: shutdown
});

e.status = 404;

handler(e, testReq(), {
send: function send(status) {
t.equal(status, 404,
'res.send() should be called ' +
'with error status.');
}),
res = testRes({
send: function send() {
t.equal(res.statusCode, status,
'res.statusCode should be set to err.status');
t.end();
},
format: format
}, testNext);
});

e.status = status;

handler(e, testReq(), res, testNext);
});

test('Default error status for non-user error',
Expand All @@ -145,19 +153,21 @@ test('Default error status for non-user error',
e = new Error(),
handler = createHandler({
shutdown: shutdown
}),
status = 511,
defaultStatus = 500,
res = testRes({
send: function send() {
t.equal(res.statusCode, defaultStatus,
'res.statusCode should be set to default status');
t.end();
},
format: format
});

e.status = 511;
e.status = status;

handler(e, testReq(), {
send: function send(status) {
t.equal(status, 500,
'res.send() should be called ' +
'with default status.');
t.end();
},
format: format
}, testNext);
handler(e, testReq(), res, testNext);
});

test('Custom timeout',
Expand Down Expand Up @@ -305,10 +315,7 @@ test('JSON error format',
e.status = 500;

handler(e, testReq(), {
send: function send(status, obj) {
t.equal(obj.status, 500,
'res.send() should be called ' +
'with status code as first param.');
send: function send(obj) {
t.equal(obj.status, 500,
'res.send() should be called ' +
'with error status on response body.');
Expand Down Expand Up @@ -336,7 +343,7 @@ test('JSON with custom error message',
e.message = 'half baked';

handler(e, testReq(), {
send: function send(status, obj) {
send: function send(obj) {
t.equal(obj.message, 'half baked',
'res.send() should be called ' +
'with custom error message.');
Expand Down Expand Up @@ -370,7 +377,7 @@ test('JSON with serializer',
e.status = 500;

handler(e, testReq(), {
send: function send(status, obj) {
send: function send(obj) {
t.equal(obj.links[0].self, '/foo',
'Should be able to define a custom ' +
'serializer for error responses.');
Expand Down

0 comments on commit 75cbe26

Please sign in to comment.