Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

node_modules
coverage
.idea
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ app.use('/api', api);

### .use(errorHandler([options]))

Currently no options.
Returns middleware that handle errors.

#### Options

The `errorHandler` function takes an option `options` object that may contain any of
the following keys:

##### log

The `log` option, if supplied, is called as `log(err, req, res)` when errors with status 500 and above occur.

### Errors

Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ var statuses = require('statuses');

var production = process.env.NODE_ENV === 'production';

module.exports = function () {
var defaultLog = function (err) {
console.error(err.stack);
};

module.exports = function (options) {
var opts = options || {};
var log = opts.log || defaultLog;

return function apiErrorHandler(err, req, res, next) {
var status = err.status || err.statusCode || 500;
if (status < 400) status = 500;
Expand All @@ -19,7 +26,7 @@ module.exports = function () {

// internal server errors
if (status >= 500) {
console.error(err.stack);
log(err, req, res);
body.message = statuses[status];
res.json(body);
return;
Expand Down