npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kazaar/express-error-handler

v3.2.2

Published

Express error handling and logging utilities

Downloads

121

Readme

Express error handler

HTTP error handling middlewares for Express

Description

This module exposes various middlewares and methods to handle errors inside an Express application:

  • HTTP error handling middleware
  • celebrate/joi error parsing middleware
  • Sequelize error parsing middleware
  • Generic server error handler
  • Sequelize connection error handler

Installation

npm install @kazaar/express-error-handler

Example

Example using all the exposed methods / middlewares:

// index.js

const express = require('express');
const expressErrorHandler = require('express-error-handler');
const logger = require('./src/config/winston');

const errorHandler = expressErrorHandler(logger);
const app = express();

// Configure application middlewares
app.use(errorHandler.celebrateErrorHandler);
app.use(errorHandler.sequelizeErrorHandler);
app.use(errorHandler.httpErrorHandler);

// Try database authentication and start server
sequelize
  .authenticate(() => {
    app
      .listen(8080, () => logger.info('Application running on port 8080'));
      .on('error', errorHandler.handleServerError)
  })
  .catch(errorHandler.handleDatabaseConnectionError);

Usage

Initialization

  • With a logging library

If you use a logging library such as Winston, import the logger and initialize the error handler with it.

// index.js

const express = require('express');
const expressErrorHandler = require('express-error-handler');
const logger = require('winston'); // or import custom Winston config

const app = express();
const errorHandler = expressErrorHandler(logger);

The logger object must have an error method (e.g logger.error()).

Compatible logging libraries: Winston, Bunyan, Pino, log4js.

  • Without a logging library

If you don't use a logging library, the handler will use the console as logger.

// index.js

const express = require('express');
const expressErrorHandler = require('express-error-handler');

const app = express();
const errorHandler = expressErrorHandler();

API

When initializing the error handler, the returned object exposes some Express middlewares as well as some error handlers.

Express middlewares

  • celebrateErrorHandler(err, req, res, next)

celebrate/joi error parsing Express middleware

const { celebrateErrorHandler } = expressErrorHandler(logger);

app.use(celebrateErrorHandler);

Middleware that checks if err was originated by celebrate (validation error) and if so, sets error status to 400 and sets error message to default Joi message or custom message if Joi.error() was used.

  • sequelizeErrorHandler(err, req, res, next)

sequelize error parsing Express middleware

const { sequelizeErrorHandler } = expressErrorHandler(logger);

app.use(sequelizeErrorHandler);

Middleware that checks if err was originated by sequelize and if so, sets error status to 500 and sets error message to default custom message parsed from error details.

  • httpErrorHandler(err, req, res, next)

HTTP error handling Express middleware

const { httpErrorHandler } = expressErrorHandler(logger);

app.use(httpErrorHandler);

Middleware used to:

  • Parse an error to get a normalized HTTP error
  • Log the error with a customized error message
  • Send the response back to the client

Important: this middleware should be configured at the end of the middlewares stack as it ends the response.

Error handlers

  • handleServerError(err)

Error handler for server 'error' event

Handler that will switch between error code property to output a custom error message.

const { handleServerError } = expressErrorHandler(logger);

const port = 8080;

app
  .listen(port, () => logger.info(`Application running on port ${port}`));
  .on('error', handleServerError)
  • handleSequelizeConnectionError(err)

Error handler for sequelize connection error

Handler that will switch between error code property to output a custom error message.

const { handleDatabaseConnectionError } = expressErrorHandler(logger);

sequelize.authenticate().catch(handleDatabaseConnectionError);

Environment

This package makes use ot the NODE_ENV environement variable.

If NODE_ENV is set to production:

  • Error stack will not be appended to the logs
  • Internal server error details will not be sent back to the client

License

MIT © Arthur Fauquenot