Skip to main content
Version: 4.x

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"
}
MemberWhat it holds
typeA URI that names the kind of problem. Each one the framework sends leads to a page listed below.
titleThe HTTP status in words.
statusThe HTTP status code.
detailWhat went wrong in this request.
instanceThe path of the request, without its query string.
errorCodeA 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-Type the 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 as AppError.notFound("User", id), for a failure you expect. Its message is sent as detail.
  • Any other error is answered with 500 and written to the log. Its message is sent as detail outside production only; in production detail is An 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​

TypeStatusMeaning
bad-request400The request could not be understood as sent.
unauthorized401The request carries no valid credentials.
forbidden403The caller is known and is not allowed to do this.
not-found404No route, or no resource, matches the request.
conflict409The request conflicts with the current state of a resource.
unsupported-media-type415The route does not accept the request's Content-Type.
validation-failed400 or 422A value in the request broke a rule of its schema.
internal-server-error500The 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.

CodeFailureArea
EXP1001Dependency is not boundDependency injection
EXP1002Constructor parameter type is unknownDependency injection
EXP2001Request body was not parsedHTTP and routing
EXP2002Unhandled error in a request handlerHTTP and routing
EXP2003Route not foundHTTP and routing
EXP2004Unsupported media typeHTTP and routing
EXP3001Body type is unknown at runtimeValidation
EXP3002Validation package is not availableValidation
EXP3003Request failed validationValidation
EXP3004No validator can apply the schemaValidation
EXP4001Port is in useConfiguration and boot
EXP4002Controller is in no moduleConfiguration and boot

Development warnings are printed once each, outside production. Set EXPRESSOTS_DEV_WARNINGS=off to silence them.