For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/server/essentials/errors.md.

Errors

Every error response Navios produces is application/problem+json (RFC 9457), unless the endpoint declares its own body for that status in errorSchema.

{
  "type": "about:blank",
  "title": "Not Found",
  "status": 404,
  "detail": "User not found"
}

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.

  1. An error mapper registered with app.mapError for the error's class or one of its parent classes. The built-in handling of steps 2 to 4 counts as registered for HttpException, GuardDeniedError and the request ValidationError, so a mapper for a class above those, such as Error, does not apply to them.
  2. An HttpException: its status, with a declared or problem body. One with a status of 500 or above is also logged.
  3. A guard that returned false: the Forbidden responder (403).
  4. A request validation error: the validation responder (400).
  5. 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.

import { ConflictException, HttpException, NotFoundException } from '@navios/server'

throw new NotFoundException('User not found')
throw new ConflictException('Email is already registered')
throw new HttpException(418, 'I am a teapot')
throw new HttpException(409, { reason: 'email-taken' })

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 endpoint declares the status in errorSchemaresponse isSent
yesan objectWhat the declared schema makes of the object
noa stringA problem with detail set to the string
noan objectA problem with the object's fields merged in
yesa stringA problem with detail set to the string

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:

ClassStatus
BadRequestException400
UnauthorizedException401
ForbiddenException403
NotFoundException404
ConflictException409
UnprocessableEntityException422
TooManyRequestsException429
InternalServerErrorException500

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.

import { problem, reply } from '@navios/server'

export class UserNotFound extends Error {
  constructor(readonly userId: string) {
    super(`User ${userId} not found`)
  }
}

// The body getUser declares for 404
app.mapError(UserNotFound, (error) => reply(404, { message: error.message }))

// Or a problem response with an extra field
app.mapError(UserNotFound, (error) => problem(404, error.message, { userId: error.userId }))
  • The mapper receives the error and the RequestContext, and returns reply(status, body), problem(status, detail?, extra?) or a full RouteResult. It may be asynchronous.
  • The most specific registered class wins: with mappers for DomainError and for UserNotFound extends DomainError, a UserNotFound uses its own mapper.
  • A mapper's reply() is sent as JSON as given. It is not checked against the endpoint's errorSchema and its status does not have to be declared.
  • A mapper registered for exactly HttpException, GuardDeniedError or ValidationError, 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 as Error, does not: an HttpException, 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:

{
  "type": "about:blank",
  "title": "Internal Server Error",
  "status": 500,
  "detail": "An unexpected error occurred"
}

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:

const app = await NaviosFactory.create(AppModule, {
  adapter: defineFastifyEnvironment(),
  exposeErrorDetails: process.env.NODE_ENV !== 'production',
})

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:

const app = await NaviosFactory.create(AppModule, {
  adapter: defineFastifyEnvironment(),
  clientErrorStacks: process.env.CLIENT_ERROR_STACKS ? 'always' : 'development',
})

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:

{
  "type": "about:blank",
  "title": "Bad Request",
  "status": 400,
  "detail": "Request validation failed for body",
  "source": "body",
  "errors": [{ "path": "email", "message": "Invalid email address" }]
}
  • source is the part of the request that failed: urlParams, query or body.
  • errors has 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 empty path.

In code, the error is a ValidationError with source and issues (the Standard Schema issues). It can be mapped like any other class:

import { problem, ValidationError } from '@navios/server'

app.mapError(ValidationError, (error) => {
  if (error.source === 'response') {
    throw error
  }
  return problem(422, 'Validation failed', {
    fields: error.issues.map((issue) => issue.message),
  })
})

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:

TokenUsed forDefault
NotFoundResponderTokena request that matches no route404 problem
ForbiddenResponderTokena guard returned false403 problem
ValidationErrorResponderTokenrequest validation failed400 problem with source and errors
InternalServerErrorResponderTokenan unexpected error500 problem

A responder implements ErrorResponder and is put in place with a provider:

import { Injectable, NotFoundResponderToken, provide } from '@navios/server'

import type { ErrorResponder, RouteResult } from '@navios/server'

@Injectable()
class JsonNotFound implements ErrorResponder {
  getResponse(_error: unknown, description?: string): RouteResult {
    return { status: 404, body: { error: 'not_found', description }, kind: 'json' }
  }
}

const app = await NaviosFactory.create(AppModule, {
  adapter: defineFastifyEnvironment(),
  providers: [provide(NotFoundResponderToken, JsonNotFound)],
})

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.