Backend: normalize errors with a single Express handler

7949
0

Without a centralized error handler, you end up with a mix of thrown errors, ad-hoc res.status(500) blocks, and inconsistent JSON shapes. I use one Express error middleware that maps known errors to stable codes and logs unknown errors with request context. I also avoid leaking stack traces in production. The practical detail is ordering: the error handler must be registered after routes, and async handlers need a wrapper to forward rejections. Once this is in place, the rest of the code can throw intentional errors (like NotFound or Forbidden) and trust they’ll become consistent API responses.