Error mapping
Every OData route answers errors in the OData v4 JSON format:
{
"error": {
"code": "NotFound",
"message": "Entity 'Products' with key '9' not found",
"details": []
}
}By default, the library maps OData parse and validation errors to 400, an ODataNotImplementedError (valid OData the service does not implement, such as $root) to 501 NotImplemented, an HttpException to its own status, an error carrying a numeric status or statusCode (400–599, http-errors style) to that status, and anything else to a generic 500. The message of any other 5xx error is replaced by An unexpected error occurred., since it may carry internal details; a 501's message only describes the unsupported part of the request and is kept. The errorMapper option lets you map any exception, including your application's own error types and errors thrown by the database, to the OData error to send.
Configuring
import { ODataModule, type ODataErrorMapper } from '@nestjs-odata/core'
import { QueryFailedError } from 'typeorm'
const errorMapper: ODataErrorMapper = (err, ctx) => {
if (err instanceof AppError) {
return {
status: err.httpStatus,
code: err.code, // e.g. 'CLIENT_NOT_FOUND'
message: err.publicMessage,
target: err.field, // optional
details: err.fieldErrors?.map((f) => ({ code: f.code, message: f.message, target: f.field })),
}
}
if (err instanceof QueryFailedError && /unique/i.test(err.message)) {
return { status: 409, code: 'Conflict', message: 'The record already exists' }
}
return undefined // default mapping
}
ODataModule.forRoot({ serviceRoot: '/odata', errorMapper })To inject services into the mapper (a translator, a logger), build it in ODataModule.forRootAsync({ useFactory, inject }).
API
type ODataErrorMapper = (
exception: unknown,
context: ODataErrorMapperContext,
) => ODataErrorMapping | undefined | null
interface ODataErrorMapping {
status: number // 400–599
code: string
message: string
target?: string
details?: { code: string; message: string; target?: string }[]
innererror?: Record<string, unknown>
language?: string // RFC 5646 tag of `message`, sent as Content-Language
}
interface ODataErrorMapperContext {
host?: ArgumentsHost // the failing route (a $batch operation's own route too)
request?: unknown // the request (a $batch operation's own request)
// Set for $batch errors: the failing operation, or { changeset: true } when a changeset
// fails as a whole (for example its commit)
batch?: { method?: string; url?: string; contentId?: string; changeset?: boolean }
}The result is sent as { "error": { code, message, target?, details, innererror? } } with HTTP status status. buildODataErrorResponse(exception, mapper?, context?, defaultLanguage?) (which returns { status, body, language }), httpStatusToODataCode(status) and odataErrorBody(code, message, details?) are exported, for custom routes that write errors themselves.
An HttpException whose body is OData-shaped ({ error: { message, details } }) keeps its message and, for a 4xx, its valid details under the default mapping.
Content-Language
OData 4.01 Part 1 §9.4 requires the language of message in the Content-Language header. Every error response of an OData route, and every $batch error part, carries it:
- the mapping's
language, when theerrorMapperreturned one; - else the
errorLanguagemodule option; - else
en, the language of the library's own messages.
ODataModule.forRoot({
serviceRoot: '/odata',
errorLanguage: 'es-CO', // your exceptions' messages are in Spanish
errorMapper: (err) =>
err instanceof GermanPartnerError
? { status: 422, code: 'Partner', message: err.message, language: 'de' }
: undefined,
})Set errorLanguage only when the messages you throw are localized: it labels the library's own (English) messages too. A language that is not a well-formed RFC 5646 tag makes the mapping invalid (default mapping, with a warning), and an invalid errorLanguage fails at startup.
Where it applies
- the
@ODataGet,@ODataGetByKey,@ODataPost,@ODataPatch,@ODataPutand@ODataDeleteroutes, wherever they are declared (they carryODataExceptionFilter; error responses are alwaysapplication/json); $metadataand the service document;- every failing operation of a
$batchrequest. Operations run through their routes, so they are mapped by the route'sODataExceptionFilterlike standalone requests, withctx.batch = { method, url, contentId }added andctx.request/ctx.hostof the operation's own request (the$batchrequest isgetODataBatchPart()?.batchRequest). The library's own operation errors (no route for the operation, a nested$batch, a target outside thebatch.entitySetsallow-list) and changeset-level failures (ctx.batch.changeset, for example a failed commit;ctx.requestis then the$batchrequest) reach the mapper too. A failed changeset is answered with the failing operation's mapped error alone.
Unmapped errors that wrap another error (new Error('…', { cause })) are mapped by their cause when it maps to a 4xx (an HttpException, an OData error, a status-carrying error); otherwise they stay a generic 500.
ODataExceptionFilter is not global and not applied to whole classes. Other routes, including plain Nest routes on an @ODataController class such as @Get('$count'), keep your application's own filters (app.useGlobalFilters(), APP_FILTER) and Nest's error format. To give one of them OData errors and the mapper, opt in per route or per class:
import { UseFilters } from '@nestjs/common'
import { ODataExceptionFilter } from '@nestjs-odata/core'
@Get('$count')
@UseFilters(ODataExceptionFilter)
count() { /* ... */ }Route-level filters win over global ones, so on the OData routes above your global filters do not run: map your error types in errorMapper instead.
Caveats
- The mapper's
message,detailsandinnererrorgo to the client as is. Don't include stack traces, SQL or secrets. - The mapper must be synchronous. Its result is validated against OData JSON Format 4.01 §21.1 and Part 1 §9.4:
statusan integer from 400 to 599,codeandmessagenon-empty strings,targeta string ornull,detailsan array of{ code, message, target? }with non-emptycodeandmessage,innererroran object. An invalid result, a mapper that throws and a mapper that returns a Promise all fall back to the default mapping, with a warning in the Nest logger (ODataErrorMappercontext). A returned Promise's rejection is handled, never left unhandled. - Errors of the
$batchenvelope itself (a missing boundary, a malformed body) are produced by the library and are not passed to the mapper.