JuniorJunior (1–3 yrs)Node.jsTCSInfosysAmazon
Express error handling
Design a production-ready Express error handling strategy (sync throw, async reject, and 404).
Answers use simple, clear English.
Quick interview answer
Wrap async routes so rejections call next(err). Central 4-arg error middleware maps domain errors to status codes, logs with requestId, hides stack in prod. 404 handler after routes.
Detailed answer
Wrap async routes so rejections call next(err). Central 4-arg error middleware maps domain errors to status codes, logs with requestId, hides stack in prod. 404 handler after routes. Never leak SQL/internal messages. Optional: boom/http-errors types.
Real example & use case
NotFoundError → 404 JSON; ValidationError → 400; unknown → 500 + correlation id.
Pros & cons
Pros: consistent API errors. Cons: must remember async wrapper.
#nodejs#express#errors