Returned errors
In Dotpress, an error is a value: a handler or a middleware returns it, like any other result.
The control flow stays visible: every way out of the handler is a return, readable from top to bottom. There is no exception to catch elsewhere, and no error class to declare.
One format for every error
Every error response has the same shape, whoever produced it: your handler, a middleware, the validation, the router or an unexpected exception.
Clients handle every error of the API with one piece of code.
The helpers
Each takes an optional message and optional data. For any other case, errorResponse() takes a status, a code, a message and data:
Thrown errors work too
Returning is the convention, but a thrown HttpError gives the same response, from a handler, a middleware or a helper of the routes.
Services do not throw HttpError: they throw their own business errors, and the handler translates them into the HTTP error the client receives. See Errors of your services.
Unexpected errors
Anything else that is thrown, a bug or a database failure, is an unexpected error:
- the client gets a
500 INTERNAL_ERROR, without details, so that nothing internal leaks; - the error is logged with the request ID, and passed to the
onErrorhook of the app and of the plugins, to report it (Sentry, for instance).
In development, createApp({ exposeErrors: true }) adds the message and the stack of the error to the 500 responses. Never enable it in production.
See Errors for the errors produced by Dotpress itself (404, 405, 413…) and for error reporting, and the design decision for the reasons behind this choice.