express-error-handler
A graceful error handler for Express applications.
Quick start:
var express = require('express'),
errorHandler = require('../error-handler.js'),
app = express(),
env = process.env,
port = env.myapp_port || 3000,
http = require('http'),
server;
app.get('/error', function createError(req,
res, next) {
var err = new Error('Sample error');
err.status = 500;
next(err);
});
server = http.createServer(app);
app.use(function (err, req, res, next) {
console.log(err);
next(err);
});
app.use( errorHandler({server: server}) );
server.listen(port, function () {
console.log('Listening on port ' + port);
});
Configuration errorHandler(options)
Here are the parameters you can pass into the errorHandler()
middleware:
-
@param {object} [options]
-
@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
-
@param {object} [options.static] Static files to send in response to specific status codes. Specify a default with options.static.default.
-
@param {number} [options.timeout] Delay between the graceful shutdown attempt and the forced shutdown timeout.
-
@param {number} [options.exitStatus] Custom process exit status code.
-
@param {object} [options.server] The app server object for graceful shutdowns.
-
@param {function} [options.shutdown] An alternative shutdown function if the graceful shutdown fails.
-
@param {function} serializer a function to customize the JSON error object. Usage: serializer(err) return errObj
-
@return {function} errorHandler Express error handling middleware.
See the tests for more examples.
errorHandler.isClientError(status)
Return true if the error status represents a client error that should not trigger a restart.
- @param {number} status
- @return {boolean}
Example
errorHandler.isClientError(404);
errorHandler.isClientError(500);
errorHandler.httpError(status, [message])
Take an error status and return a route that sends an error with the appropriate status and message to an error handler via next(err)
.
- @param {number} status
- @param {string} message
- @return {function} Express route handler
*
* app.get( '/foo', handleFoo() );
*
* app.all( '/foo', createHandler.httpError(405) );
Thanks