Middleware for Express that catches body-parser errors and returns appropriate 4xx HTTP responses with descriptive messages. v1.0.9 is stable, last updated in 2021. Handles all body-parser error types (invalid JSON, payload too large, unsupported charset, etc.) and maps them to correct status codes (400, 413, 415, 500). Ships TypeScript definitions. Unlike manual error handling in Express, this provides a consistent, one-line middleware approach. No breaking changes since v1.0.0.
npm install express-body-parser-error-handlerVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup: import, placement after body-parsers, and optional onError/errorMessage configuration for custom responses.
Always place app.use(bodyParserErrorHandler()) after all body-parser middleware calls (e.g., json(), urlencoded()).
In your onError function, always call next(err) (or next(error)) after your custom logic.
Always return a string from errorMessage, even if you want to include the original: `return err.message`.
Use Express's built-in error handler or a separate middleware for general errors.
Use `import bodyParserErrorHandler from 'express-body-parser-error-handler'` (no braces).
Ensure body-parser (or express.json, etc.) is used before this middleware and that body-parser is in your dependencies.
Run `npm install express-body-parser-error-handler`. For ESM, use import syntax or set `"type": "module"` in package.json.