express-error-handler
Advanced tools
Comparing version 0.5.4 to 0.6.0
@@ -97,7 +97,7 @@ /** | ||
send = function send(statusCode, err, res, o) { | ||
var body = mixIn({}, { | ||
var body = { | ||
status: statusCode, | ||
message: err.message || | ||
statusCodes[statusCode] | ||
}); | ||
}; | ||
@@ -108,3 +108,3 @@ body = (o.serializer) ? | ||
res.send(statusCode, body); | ||
res.status(statusCode).send(body); | ||
}, | ||
@@ -233,9 +233,21 @@ | ||
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; | ||
} | ||
}); | ||
} | ||
@@ -242,0 +254,0 @@ }); |
@@ -33,3 +33,3 @@ 'use strict'; | ||
// all you need to do is send the response: | ||
res.send(200); | ||
res.status(200).end(); | ||
}); | ||
@@ -36,0 +36,0 @@ |
{ | ||
"name": "express-error-handler", | ||
"version": "0.5.4", | ||
"version": "0.6.0", | ||
"description": "A graceful error handler for Express applications.", | ||
@@ -27,16 +27,17 @@ "main": "error-handler.js", | ||
"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", | ||
"bunyan-request-logger": "~0.1.1", | ||
"connect-cache-control": "~0.1.0", | ||
"express": "^4.9.8", | ||
"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" | ||
"restify": "~2.6.0", | ||
"supertest": "~0.8.0", | ||
"tape": "~1.1.1", | ||
"through": "~2.3.4" | ||
} | ||
} |
@@ -60,2 +60,3 @@ express-error-handler | ||
* @param {function} serializer a function to customize the JSON error object. Usage: serializer(err) return errObj | ||
* @param {function} framework Either 'express' (default) or 'restify'. | ||
* @return {function} errorHandler Express error handling middleware. | ||
@@ -69,37 +70,40 @@ | ||
```js | ||
var errorHandler = require('express-error-handler'), | ||
handler = errorHandler({ | ||
handlers: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
var errorHandler = require('express-error-handler'), | ||
handler = errorHandler({ | ||
handlers: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
// After all your routes... | ||
// Pass a 404 into next(err) | ||
app.use( errorHandler.httpError(404) ); | ||
// After all your routes... | ||
// Pass a 404 into next(err) | ||
app.use( errorHandler.httpError(404) ); | ||
// Handle all unhandled errors: | ||
app.use( handler ); | ||
// Handle all unhandled errors: | ||
app.use( handler ); | ||
``` | ||
Or for a static page: | ||
handler = errorHandler({ | ||
static: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
```js | ||
handler = errorHandler({ | ||
static: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
``` | ||
Or for a custom view: | ||
handler = errorHandler({ | ||
views: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
```js | ||
handler = errorHandler({ | ||
views: { | ||
'404': function err404() { | ||
// do some custom thing here... | ||
} | ||
} | ||
}); | ||
``` | ||
@@ -142,9 +146,15 @@ | ||
Restify error handling works different from | ||
Express. | ||
Restify error handling works different from Express. To trigger restify mode, you'll need to pass the `framework` parameter when you create the errorHandler: | ||
First, `next(err)` is synonymous with `res.send(status, error)`. This means that you should *only use `next(err)` to report errors to users*, and not as a way to aggregate errors to a common error handler. Instead, you can invoke an error handler directly to aggregate your error handling in one place. | ||
```js | ||
var handleError = errorHandler({ | ||
server: server | ||
framework: 'restify' | ||
}); | ||
``` | ||
* There is no error handling middleware. Instead, use `server.on(`uncaughtException`, handleError)` | ||
In restify, `next(err)` is synonymous with `res.send(status, error)`. This means that you should *only use `next(err)` to report errors to users*, and not as a way to aggregate errors to a common error handler. Instead, you can invoke an error handler directly to aggregate your error handling in one place. | ||
There is no error handling middleware. Instead, use `server.on('uncaughtException', handleError)` | ||
See the examples in `./examples/restify.js` | ||
@@ -151,0 +161,0 @@ |
@@ -6,2 +6,3 @@ 'use strict'; | ||
through = require('through'), | ||
mixIn = require('mout/object/mixIn'), | ||
@@ -14,7 +15,12 @@ format = function format (types) { | ||
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); | ||
}, | ||
@@ -106,2 +112,9 @@ testNext = function () {}; | ||
} | ||
}), | ||
res = testRes({ | ||
render: function render() { | ||
t.pass('Render should be called for ' + | ||
'custom views.'); | ||
t.end(); | ||
} | ||
}); | ||
@@ -111,9 +124,3 @@ | ||
handler(e, testReq(), { | ||
render: function render() { | ||
t.pass('Render should be called for ' + | ||
'custom views.'); | ||
t.end(); | ||
} | ||
},testNext); | ||
handler(e, testReq(), res, testNext); | ||
}); | ||
@@ -128,17 +135,18 @@ | ||
e = new Error(), | ||
status = 404, | ||
handler = createHandler({ | ||
shutdown: shutdown | ||
}), | ||
res = testRes({ | ||
send: function send() { | ||
t.equal(res.statusCode, status, | ||
'res.statusCode should be set to err.status'); | ||
t.end(); | ||
}, | ||
format: format | ||
}); | ||
e.status = 404; | ||
e.status = status; | ||
handler(e, testReq(), { | ||
send: function send(status) { | ||
t.equal(status, 404, | ||
'res.send() should be called ' + | ||
'with error status.'); | ||
t.end(); | ||
}, | ||
format: format | ||
}, testNext); | ||
handler(e, testReq(), res, testNext); | ||
}); | ||
@@ -153,15 +161,17 @@ | ||
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); | ||
}); | ||
@@ -309,13 +319,7 @@ | ||
shutdown: shutdown | ||
}); | ||
e.status = 500; | ||
handler(e, testReq(), { | ||
send: function send(status, obj) { | ||
}), | ||
res = testRes({ | ||
send: function send(obj) { | ||
t.equal(obj.status, 500, | ||
'res.send() should be called ' + | ||
'with status code as first param.'); | ||
t.equal(obj.status, 500, | ||
'res.send() should be called ' + | ||
'with error status on response body.'); | ||
@@ -326,7 +330,11 @@ t.equal(obj.message, 'Internal Server Error', | ||
t.end(); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}, testNext); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}); | ||
e.status = 500; | ||
handler(e, testReq(), res, testNext); | ||
}); | ||
@@ -341,2 +349,13 @@ | ||
shutdown: shutdown | ||
}), | ||
res = testRes({ | ||
send: function send(obj) { | ||
t.equal(obj.message, 'half baked', | ||
'res.send() should be called ' + | ||
'with custom error message.'); | ||
t.end(); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}); | ||
@@ -347,13 +366,3 @@ | ||
handler(e, testReq(), { | ||
send: function send(status, obj) { | ||
t.equal(obj.message, 'half baked', | ||
'res.send() should be called ' + | ||
'with custom error message.'); | ||
t.end(); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}, testNext); | ||
handler(e, testReq(), res, testNext); | ||
}); | ||
@@ -378,2 +387,13 @@ | ||
} | ||
}), | ||
res = testRes({ | ||
send: function send(obj) { | ||
t.equal(obj.links[0].self, '/foo', | ||
'Should be able to define a custom ' + | ||
'serializer for error responses.'); | ||
t.end(); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}); | ||
@@ -383,13 +403,3 @@ | ||
handler(e, testReq(), { | ||
send: function send(status, obj) { | ||
t.equal(obj.links[0].self, '/foo', | ||
'Should be able to define a custom ' + | ||
'serializer for error responses.'); | ||
t.end(); | ||
}, | ||
format: function format (types) { | ||
return types['json'](); | ||
} | ||
}, testNext); | ||
handler(e, testReq(), res, testNext); | ||
}); |
28624
773
164
3
+ Addedjson-stringify-safe@^5.0.0
+ Addedjson-stringify-safe@5.0.1(transitive)