Skip to content

RouterExceptionFilters

reference
1 min readUpdated

Kind: Class

Source: packages/core/router/router-exception-filters.ts

Part of: Core

RouterExceptionFilters builds an ExceptionsHandler for a routed controller method. It resolves method-, controller-, and application-level exception filters, including scoped global filters, and attaches them in the correct execution order.

Extends: BaseExceptionFilterContext

Methods

MethodSignatureReturns
create`create(instance: Controller, callback: RouterProxyCallback, moduleKey: stringundefined, contextId: undefined, inquirerId: string)`
getGlobalMetadatagetGlobalMetadata(contextId: undefined, inquirerId: string)T

Where it refuses work

  • RouterExceptionFilters stops the work with an early return when isEmpty(filters).
  • RouterExceptionFilters stops the work with an early return when contextId === STATIC_CONTEXT && !inquirerId.

Diagram

mermaid
graph LR
  A[Incoming route handler] --> B[RouterExceptionFilters.create]
  B --> C[Resolve method/controller filter metadata]
  B --> D[Resolve global filters]
  C --> E[ExceptionsHandler]
  D --> E
  E --> F[Execute matching exception filter]
  F --> G[Send HTTP error response]

Usage

ts
import { RouterExceptionFilters } from '@nestjs/core/router/router-exception-filters';

// Typically created internally by Nest during application bootstrap.
const exceptionFilters = new RouterExceptionFilters(
  container,
  applicationConfig,
  httpAdapter,
);

// Create the handler used to process exceptions thrown by a route callback.
const handler = exceptionFilters.create(
  usersController,
  usersController.findOne,
  moduleKey,
);

// The returned ExceptionsHandler is used by the router proxy.
try {
  await usersController.findOne('123');
} catch (error) {
  handler.next(error, requestContext);
}

AI Coding Instructions

  • Treat RouterExceptionFilters as router infrastructure; application code should normally register filters with @UseFilters() or app.useGlobalFilters() instead of instantiating it directly.
  • Preserve filter resolution order when changing this class: method and controller metadata must be combined with global filter metadata predictably.
  • Ensure request-scoped and transient global filters are resolved using the active context ID and inquirer ID when applicable.
  • Return an ExceptionsHandler even when no custom filters are registered so default HTTP exception handling remains available.
  • Keep integrations aligned with ApplicationConfig, NestContainer, and the active HTTP adapter, since they provide global filters, dependency resolution, and response handling.

Relationships

  • IMPORTS → HttpServer
  • IMPORTS → EXCEPTION_FILTERS_METADATA
  • IMPORTS → Controller
  • IMPORTS → isEmpty

Was this page helpful?

Download as PDF
RouterExceptionFilters — NestJS head-to-head