How do I handle errors in Express?
Add an error-handling middleware (four parameters) at the end of app.js. Throw or call next(err) in handlers. Use a custom ApiError class with status and message. Use an async wrapper to catch promise rejections. Log externally in production.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Handle Errors in an Express Server
Express 4 was designed before async/await and does not catch promise rejections from async handlers. Use an asyncHandler wrapper that catches and passes errors to next. Express 5 catches async errors automatically.
400 bad request, 401 not authenticated, 403 not authorized, 404 not found, 409 conflict (duplicate), 422 unprocessable entity, 500 server error, 503 maintenance or downstream failure.
Stack traces expose internal file paths, library versions, and logic. Attackers can use them to find weaknesses. In production, send a safe message; log the full error internally.
Still have questions?
Browse all our FAQs or reach out to our support team
