Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including express-api-error-handler with all npm packages installed. Try it out:

var expressApiErrorHandler = require("express-api-error-handler")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

express-api-error-handler v1.1.0

Error handler for express APIs

express-api-error-handler

Build Status codecov

express-api-error-handler is a simple library for error handling in express apps It's compatible with joi and http-errors;

Installation

npm i --save express-api-error-handler

Sample usage with bunyan as a logger

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);


Errors from http-errors

GET http://localhost:3000/error

Response:
Alt text

Console log:
Alt text

Errors from Joi

GET http://localhost:3000/joi

Response:
Alt text

Console log:
Alt text

Not found routes

GET http://localhost:3000/foobar

Response:
Alt text

Console log:
Alt text

API

Error handler

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,
})

Not found handler

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free