Installation


Get started with ds-express-errors in seconds.

Install via NPM

npm install ds-express-errors

Current latest version:NPM Version

Basic Setup

Integrate the global error handler into your Express application.

Warning

The errorHandler middleware must be defined last, after all other routes and middleware.


About Environment and Prod vs Dev Responses

By default, the library relies on the NODE_ENV environment variable to format error responses.

If NODE_ENV is not defined, you will receive sanitised error messages, as the library behaves as if the environment were set to production.

To see how Production and Development responses differ, visit the Configuration part

See the devEnvironments property to customize which environments should display full error information (stack traces and full error messages)


Good to know

If you use only errorHandler the library would perform only duck-typing checks.

Want strict checks? Visit Configuration part. There you will find config property errorClasses.


javascript

import express from 'express'
import { errorHandler } from 'ds-express-errors'

const app = express();

// ... define your routes here ...

// Add Error Handler at the very end
app.use(errorHandler);

app.listen(3000, () => {
  console.log('Server is running');
});

Have some ideas? Find something missing? or Bug?

Share your suggestions with us!

Want to see ds-express-errors in actions? Watch the video tutorial on YouTube.



The video covers:

  • Global Express.js error handling with errorHandler
  • Automatic error recognition
  • Duck typing and strict error detection
  • Selecting library mappers with needMappers
  • Passing original library objects through errorClasses
  • Strict Prisma and Zod error handling
  • Custom API response structures with formatError
  • Creating a JSON:API-style error response
  • Using the err, req, and isDev parameters
  • Configuring development environments with devEnvironments
  • Protecting sensitive error details in production
  • Development and production error responses

Good to know

This video was recorded before version 1.9.2, which reduced excessive production response sanitization. This does not affect the video or its relevance. Just keep in mind that production error responses are now clearer and more informative than they were at the time of recording.


Link: Official ds-express-errors channel