Error responses
Since 4.4.0 a failed request can be answered in one shape whatever caught the failure: an RFC 9457 problem document, sent as application/problem+json.
{
"type": "https://doc.expresso-ts.com/docs/errors/not-found",
"title": "Not Found",
"status": 404,
"detail": "User with id 42 not found",
"instance": "/api/users/42"
}
| Member | What it holds |
|---|---|
type | A URI that names the kind of problem. Each one the framework sends leads to a page listed below. |
title | The HTTP status in words. |
status | The HTTP status code. |
detail | What went wrong in this request. |
instance | The path of the request, without its query string. |
errorCode | A framework code, present when the framework itself detected the failure. |
Turning it on
Call setErrorHandler with problemJson in configureServices():
async configureServices(): Promise<void> {
this.Middleware.parse();
this.Middleware.setErrorHandler({ problemJson: true });
}
With it, these are all answered as a problem document, in every environment:
- an error a handler or middleware throws,
- a request for a route that does not exist (404),
- a request whose
Content-Typethe route does not accept (415), - a request that fails validation (400, with the failures in
errors).
The document also keeps code and error, the two members of the response setErrorHandler() sent before 4.4.0, so a client that reads them keeps working. The examples on these pages leave them out.
An application built with micro() has the same option on the application itself:
const app = micro();
app.setErrorHandler({ problemJson: true });
Projects created with expressots new call it already, from 4.4.0.
With no error handler registered
An application of either kind that never calls setErrorHandler answers an uncaught error with a problem document outside production only. In production it answers as it did before 4.4.0, with Express's own HTML page. That default is planned to change in 5.0.0; until then, call setErrorHandler({ problemJson: true }) to get the same answer everywhere.
What reaches the client
- Throw
new AppError(message, status), or one of its helpers such asAppError.notFound("User", id), for a failure you expect. Its message is sent asdetail. - Any other error is answered with 500 and written to the log. Its message is sent as
detailoutside production only; in productiondetailisAn unexpected error occurred. - An error from middleware that carries a 4xx status, such as the body parser's error for malformed JSON, keeps that status.
Problem types
| Type | Status | Meaning |
|---|---|---|
bad-request | 400 | The request could not be understood as sent. |
unauthorized | 401 | The request carries no valid credentials. |
forbidden | 403 | The caller is known and is not allowed to do this. |
not-found | 404 | No route, or no resource, matches the request. |
conflict | 409 | The request conflicts with the current state of a resource. |
unsupported-media-type | 415 | The route does not accept the request's Content-Type. |
validation-failed | 400 or 422 | A value in the request broke a rule of its schema. |
internal-server-error | 500 | The application failed in a way it did not expect. |
An AppError with any other status has the type about:blank, which RFC 9457 defines as a problem with no meaning beyond its status code.
The type URIs changed in 4.4.0. Earlier releases sent https://expressots.dev/errors/..., a domain the project does not own. A client that compares type against the old value needs the new one; see the upgrade guide.
Framework codes
An error or development warning that the framework raises ends with a line Code: EXPnnnn. With JSON logs (EXPRESSOTS_LOG=json) the code is the field code. The wording of a message can change between releases; its code does not.
| Code | Failure | Area |
|---|---|---|
EXP1001 | Dependency is not bound | Dependency injection |
EXP1002 | Constructor parameter type is unknown | Dependency injection |
EXP2001 | Request body was not parsed | HTTP and routing |
EXP2002 | Unhandled error in a request handler | HTTP and routing |
EXP2003 | Route not found | HTTP and routing |
EXP2004 | Unsupported media type | HTTP and routing |
EXP3001 | Body type is unknown at runtime | Validation |
EXP3002 | Validation package is not available | Validation |
EXP3003 | Request failed validation | Validation |
EXP3004 | No validator can apply the schema | Validation |
EXP4001 | Port is in use | Configuration and boot |
EXP4002 | Controller is in no module | Configuration and boot |
Development warnings are printed once each, outside production. Set EXPRESSOTS_DEV_WARNINGS=off to silence them.