express-error-handler
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -184,3 +184,3 @@ /** | ||
defaultStatic = o.static['default'], | ||
status = err.status, | ||
status = err.status || res.statusCode, | ||
handler = o.handlers[status], | ||
@@ -187,0 +187,0 @@ view = o.views[status], |
{ | ||
"name": "express-error-handler", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "A graceful error handler for Express applications.", | ||
@@ -5,0 +5,0 @@ "main": "error-handler.js", |
@@ -53,3 +53,2 @@ express-error-handler | ||
* @param {object} [options.handlers] Custom handlers for specific status codes. | ||
* @param {object} [options.views] View files to render in response to specific status codes. Specify a default with `options.views.default` | ||
@@ -64,5 +63,47 @@ * @param {object} [options.static] Static files to send in response to specific status codes. Specify a default with options.static.default. | ||
See the tests for more examples. | ||
### Examples: | ||
`express-error-handler` lets you specify custom templates, static pages, or error handlers for your errors. It also does other useful error-handling things that every app should implement, like protect against 4xx error DOS attacks, and graceful shutdown on unrecoverable errors. Here's how you do what you're asking for: | ||
```js | ||
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) ); | ||
// Handle all unhandled errors: | ||
app.use( handler ); | ||
Or for a static page: | ||
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... | ||
} | ||
} | ||
}); | ||
``` | ||
[More examples](https://github.com/dilvie/express-error-handler/tree/master/examples) are available in the examples folder. | ||
## errorHandler.isClientError(status) | ||
@@ -93,6 +134,6 @@ | ||
```js | ||
* // Define supported routes | ||
* app.get( '/foo', handleFoo() ); | ||
* // 405 for unsupported methods. | ||
* app.all( '/foo', createHandler.httpError(405) ); | ||
// Define supported routes | ||
app.get( '/foo', handleFoo() ); | ||
// 405 for unsupported methods. | ||
app.all( '/foo', createHandler.httpError(405) ); | ||
``` | ||
@@ -99,0 +140,0 @@ |
@@ -52,3 +52,3 @@ 'use strict'; | ||
test('Custom exit status', function (t) { | ||
test('Custom handler', function (t) { | ||
var shutdown = function shutdown() {}, | ||
@@ -74,2 +74,24 @@ e = new Error(), | ||
test('Missing error status', function (t) { | ||
var shutdown = function shutdown() {}, | ||
e = new Error(), | ||
handler = createHandler({ | ||
shutdown: shutdown, | ||
handlers: { | ||
'404': function err404() { | ||
t.pass('Should get status from ' + | ||
'res.statusCode'); | ||
t.end(); | ||
} | ||
} | ||
}), | ||
res = testRes(); | ||
res.statusCode = 404; | ||
handler( e, testReq(), res, | ||
testNext ); | ||
}); | ||
test('Custom views', function (t) { | ||
@@ -76,0 +98,0 @@ var shutdown = function shutdown() {}, |
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
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
26520
743
142