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