An Express library for centralized error handling

byβ€’

Express apps often end up with scattered error handling. Try catch in routes, inconsistent responses, duplicated logic. Centralizing this logic can make apps more predictable and easier to maintain.

I published a open source error handling library called ds-express-errors that tries to address these issues in a structured, minimal way.

Here is what it provides:

  • Centralized error middleware added after all routes, so error handling logic is not duplicated across the app.

  • Preset standard HTTP error classes like 4xx, 5xx that can be thrown directly from handlers.

  • Automatic mapping of common library errors such as Zod, Joi, Prisma, Mongoose, Sequelize and JWT into consistent HTTP responses.

  • Support for async and await errors so rejected promises are handled safely.

  • Built-in logging with optional integration of external loggers like Winston or Pino.

  • Full customization of error responses and formats to match any API standard or project requirements.

  • Global handlers for `uncaughtException` and `unhandledRejection`, with support for graceful shutdown including cleanup logic and server closure.

Basic usage is minimal. Install the package and register a single error middleware at the end of the Express app.

πŸ”— Official website & documentation: ds-express-errors

I am curious how others structure error handling in Express applications.

Do you prefer using centralized middleware or individual try/catch in routes?

How do you structure logging and responses for async errors in your apps?

How do you usually handle error handling in larger Express apps?

40 views

Add a comment

Replies

Be the first to comment