Centralized error handling keeps response logic consistent. In Express, an error middleware has four parameters: err, req, res, next.
Log detailed errors on the server, but return safe messages to users in production.
Deep dive
Operational errors vs programming errors
An invalid user request is different from a programming bug. Handle expected operational errors deliberately and log unexpected failures so they can be fixed.
Consistent error format
Clients should receive predictable error responses containing safe information such as an error code, message, and optionally field-level validation details.