#zero-dependency #lightweight
Standardize Your Express.js Error Handling
with ds-express-errors
Type-safe, centralized, and production-ready error handling for Express.js.
npm install ds-express-errorsExplore new version v1.7.1 in Changelog
Explore library Roadmap
Quick example using ds-express-errors library:
Before:
- Spaghetti code with nested
try/catch - Manual database / validation errors detection
- Manual status codes (409 vs 400?)
- Need to implement graceful shutdown separately
1app.post('/register', async (req, res, next) => {
2 try {
3 const { email, password } = req.body;
4
5 // 1. Manual Validation
6 if (!email || !password) {
7 return res.status(400).json({ error: 'Missing fields' });
8 }
9
10 const user = await User.create({ email, password });
11 res.status(201).json({ data: user });
12
13 } catch (error) {
14 // 2. Manual DB Error Handling
15 if (error.code === 11000) { // MongoDB Duplicate
16 return res.status(409).json({ error: 'Email already exists' });
17 }
18 // 3. Generic Error Handler
19 next(error);
20 }
21});After:
- Reduced spaghetti code and make more readable code
- Auto-mapped DB errors (MongoDB dublicate →
409 Conflict) - Auto-mapped validation errors (
400 Bad Request,422 Unprocessable Content, etc...) - Graceful Shutdown included
- No
try/catchneeded -asyncHandlercatches everything - Simple custom errors with
Errors.BadRequest(),Errors.NotFound(), etc...
1import { asyncHandler, Errors } from 'ds-express-errors'
2
3app.post('/register', asyncHandler(async (req, res) => {
4 const { email, password } = req.body;
5
6 // 1. DB Errors (Duplicate Email) -> Auto-mapped to 409 Conflict
7 // 2. Validation Errors (Mongoose/Zod) -> Auto-mapped to 400 Bad Request
8 const user = await User.create({ email, password });
9
10 // 3. Custom Logic? Easy.
11 if (!user) throw Errors.BadRequest('Registration failed');
12
13 res.status(201).json({ data: user });
14}));Core Features
Why ds-express-errors is the right choice for your project.
Auto-Mapping
Automatically map common error (Zod, Joi, Prisma, Mongoose, Sequelize, JWT, express-validator) types to standardized, production-ready HTTP responses.
Explore →
Graceful Shutdown
Safely handle process termination and unhandled exceptions to prevent data corruption.
Explore →
Smart Detection
Zero-Dependency Smart Detection. Robust error recognition logic that works even if libraries change error class names. responses.
Full Customizability
Fully Customizable. Adapt response structure, define custom error mappers, and configure dev environments.
Explore →
Logging
Built-in simple logger or full integration with custom loggers (Winston, Pino).
TypeScript Support
Enjoy first-class TypeScript integration for a fully type-safe development experience.
No More Try-Catch
Automatically catch async errors and rejected promises. Keep your controllers clean and readable.
Ready-to-use Presets
Jumpstart your project with pre-configured error handlers for common scenarios.
Explore →
