
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
express-api-error-handler
Advanced tools
express-api-error-handler is a simple library for error handling in express apps It's compatible with joi and http-errors;
npm i --save express-api-error-handler
You can test the below example by running npm run example
// app.js
import express from 'express';
import Joi from 'joi';
import HTTPError from 'http-errors';
import bunyan from 'bunyan';
import { errorHandler, notFoundHandler } from '../src';
const app = express();
const logger = bunyan.createLogger({ name: 'app' });
app.get('/', (req, res) => {
res.json({ ok: true });
});
app.get('/error', (req, res, next) => {
next(new HTTPError.NotFound('Object not found'));
});
app.get('/joi', (req, res, next) => {
Joi.validate({ foo: 'bar' }, { foo: Joi.number() }, next);
});
app.use(errorHandler({
log: ({ err, req, res, body }) => {
logger.error(err, `${body.status} ${req.method} ${req.url}`);
},
hideProdErrors: true, // hide 5xx errors if NODE_ENV is "production" (default: false)
}));
app.use(notFoundHandler({
log: ({ req, res }) => {
logger.error(`404 ${req.method} ${req.url}`);
},
}));
const PORT = 3000;
app.listen(PORT);
GET http://localhost:3000/error
Response:

Console log:

GET http://localhost:3000/joi
Response:

Console log:

GET http://localhost:3000/foobar
Response:

Console log:

errorHandler({
/**
* The optional handler for logging
* @param {Error} err the occured error
* @param {Object} req the request express object
* @param {Object} res the response express object
* @param {Object} body the response body
* @param {Number} body.status the returned http status
* @param {String} body.error the error message
* @param {Array} body.details the Joi validation details
* @param {Array} body.stack the stack trace (only if NODE_ENV !== 'production')
*/
log: ({ err, req, res, body }) => {
...
},
/**
* Hides 5xx errors if NODE_ENV is "production" (default: false)
*/
hideProdErrors: true/false,
})
notFoundHandler({
/**
* The optional handler for logging
* @param {Object} req the request express object
* @param {Object} res the response express object
*/
log: ({ req, res }) => {
...
},
})
MIT License
Copyright (c) 2016 Łukasz Sentkiewicz
FAQs
Error handler for express APIs
The npm package express-api-error-handler receives a total of 283 weekly downloads. As such, express-api-error-handler popularity was classified as not popular.
We found that express-api-error-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.