Errors
Every error response Navios produces is application/problem+json (RFC 9457), unless the endpoint declares its own body for that status in errorSchema.
This page describes how an error becomes that response and how to change the outcome.
How a thrown error becomes a response
Anything thrown by an interceptor, a guard, validation or a handler goes through these steps in order. The first that applies produces the response.
- An error mapper registered with
app.mapErrorfor the error's class or one of its parent classes. The built-in handling of steps 2 to 4 counts as registered forHttpException,GuardDeniedErrorand the requestValidationError, so a mapper for a class above those, such asError, does not apply to them. - An
HttpException: its status, with a declared or problem body. One with a status of 500 or above is also logged. - A guard that returned
false: the Forbidden responder (403). - A request validation error: the validation responder (400).
- Anything else: logged in full, then the internal-error responder (500).
A response body that fails its schema is not a validation error in the sense of step 4. It is the server's fault and goes to step 5.
HttpException
Throw an HttpException to answer with a specific status from anywhere in the pipeline.
new HttpException(statusCode, response, cause?) extends Error. response is a string or an object, and what is sent depends on the endpoint's errorSchema:
The body of a thrown HttpException is not type-checked against errorSchema. It is validated at run time: fields the schema does not declare are removed, and a body that does not match is logged as an error and answered as a problem of the same status, without the fields it was thrown with. For a type-checked error body, return reply(status, body) from the handler instead.
The status is one of an error, 400 to 599; the constructor throws for another, and for a response that is an array or an Error. An error goes into cause.
The subclasses, each with a default message:
Mapping domain errors
Services should throw their own errors and not know about HTTP. A mapper registered on the application turns an error class into a response.
- The mapper receives the error and the
RequestContext, and returnsreply(status, body),problem(status, detail?, extra?)or a fullRouteResult. It may be asynchronous. - The most specific registered class wins: with mappers for
DomainErrorand forUserNotFound extends DomainError, aUserNotFounduses its own mapper. - A mapper's
reply()is sent as JSON as given. It is not checked against the endpoint'serrorSchemaand its status does not have to be declared. - A mapper registered for exactly
HttpException,GuardDeniedErrororValidationError, or for a subclass of one of them, replaces the built-in handling of that class. A mapper for a parent of those classes, such asError, does not: anHttpException, a denied guard and a request validation error keep their built-in responses. - When a mapper itself throws, the error it threw goes through steps 2 to 5.
- An error that a mapper answers with a status of 500 or above is logged.
- Mappers are not consulted for requests that match no route.
app.mapError may be called before or after init() and returns the application. A plugin registers mappers through its own API.
Unexpected errors
An error that is answered with a status of 500 or above is logged with the request's method and path, unless the client has gone and the error says that the work was aborted (AbortError), as that of a call that was given ctx.signal does. So is a response that cannot be sent, such as a body without a JSON form or a header value with a line break: it is an error of the request like one that was thrown, and mappers apply to it. For step 5 the client receives:
The error's message is not sent, because it can contain connection strings, SQL or file paths. With exposeErrorDetails: true the message is sent as detail, which is useful during development:
Stacks of client errors
Capturing a stack costs more than the rest of the request pipeline, and nothing in the framework reads the stack of an error that is answered with a 4xx status. In production, these errors therefore have none: an HttpException below 500, a denied guard, and a request that fails validation. Every other error has its stack, including an HttpException of 500 or above and a response that does not match its schema.
The clientErrorStacks application option changes that. 'development', the default, captures stacks unless NODE_ENV is production when the application initialises; 'always' and 'never' do not look at the environment. To find out where a 404 comes from in production:
An exception is constructed by code that does not know which application will answer it, so the option applies to the process from init() until close(). With several applications in one process, stacks are captured while any of them wants them.
Validation errors
A request that fails validation gets a 400 problem with two extra fields:
sourceis the part of the request that failed:urlParams,queryorbody.errorshas one entry per issue reported by the schema library, with the issue's path joined by dots and its message unchanged.- A body that is not valid JSON is reported the same way, with
source: 'body'and an emptypath.
In code, the error is a ValidationError with source and issues (the Standard Schema issues). It can be mapped like any other class:
Such a mapper also receives response validation errors, which have source: 'response'. Rethrow those, as above, so that they stay a 500.
Responders
Four framework responses are produced by replaceable singletons:
A responder implements ErrorResponder and is put in place with a provider:
A responder may also return reply() or problem(). One that throws or returns something else is logged, and a plain problem is sent in its place.
description is a message from the framework: the unmatched method and path for not-found, and the error's message for the internal-error responder when exposeErrorDetails is on. Responders are resolved once at init().
Use a responder to change the format of a whole category of framework errors. Use a mapper for an error class of your own.
On the client
A client that calls the same declaration sees a declared error body as an EndpointError of kind http with a typed body. A problem response for a status the declaration does not list arrives as kind http-unknown, with the problem object as its body. See Errors in the Builder docs.