Skip to content

BaseExceptionFilterContext

reference
1 min readUpdated

Kind: Class

Source: packages/core/exceptions/base-exception-filter-context.ts

Part of: Core

BaseExceptionFilterContext resolves exception filter metadata into executable ExceptionFilter instances. It supports both direct filter objects and dependency-injected filter classes, using module context and reflection metadata to locate registered providers.

Extends: ContextCreator

Methods

MethodSignatureReturns
createConcreteContextcreateConcreteContext(metadata: T, contextId: undefined, inquirerId: string)R
getFilterInstance`getFilterInstance(filter: FunctionExceptionFilter, contextId: undefined, inquirerId: string)`
getInstanceByMetatypegetInstanceByMetatype(metatype: Type<unknown>)`InstanceWrapper
reflectCatchExceptionsreflectCatchExceptions(instance: ExceptionFilter)Type<any>[]

Properties

PropertyType
moduleContextstring

Where it refuses work

  • BaseExceptionFilterContext stops the work with an early return when isEmpty(metadata).
  • BaseExceptionFilterContext stops the work with an early return when isObject.
  • BaseExceptionFilterContext stops the work with an early return when !instanceWrapper.
  • BaseExceptionFilterContext stops the work with an early return when !this.moduleContext.
  • BaseExceptionFilterContext stops the work with an early return when !moduleRef.

Diagram

mermaid
graph LR
  A[Exception filter metadata] --> B[BaseExceptionFilterContext]
  B --> C{Filter is an object?}
  C -->|Yes| D[Use filter instance]
  C -->|No| E[Find InstanceWrapper in module injectables]
  E --> F[Resolve instance for context ID]
  D --> G[Validate catch() method]
  F --> G
  G --> H[Concrete ExceptionFilter instances]

Usage

ts
import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
} from '@nestjs/common';
import { BaseExceptionFilterContext } from '@nestjs/core/exceptions';

// A filter object can be resolved without looking up a DI provider.
@Catch(Error)
class LogErrorFilter implements ExceptionFilter {
  catch(exception: Error, host: ArgumentsHost) {
    console.error('Request failed:', exception.message);
  }
}

// In framework integration code, `container` is Nest's NestContainer instance.
const filterContext = new BaseExceptionFilterContext(container);

const filters = filterContext.createConcreteContext<
  ExceptionFilter[],
  ExceptionFilter[]
>([new LogErrorFilter()]);

// `filters` contains only valid objects that implement catch().
filters.forEach(filter => filter.catch(new Error('Example'), host));

AI Coding Instructions

  • Resolve filter classes through getFilterInstance() so request-scoped and dependency-injected providers use the correct context ID.
  • Ensure every resolved filter implements a callable catch() method; invalid or unresolved filters are excluded from the concrete context.
  • Set the active module context before resolving class-based filters, since getInstanceByMetatype() searches that module’s injectable providers.
  • Use reflectCatchExceptions() to read @Catch() metadata rather than manually inspecting decorator metadata keys.

Relationships

  • IMPORTS → FILTER_CATCH_EXCEPTIONS
  • IMPORTS → Type
  • IMPORTS → ExceptionFilter
  • IMPORTS → isEmpty
  • IMPORTS → isFunction

Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (2)

  • ExceptionFiltersContextpackages/microservices/context/exception-filters-context.ts:16
  • ExceptionFiltersContextpackages/websockets/context/exception-filters-context.ts:10

Was this page helpful?

Download as PDF
BaseExceptionFilterContext — NestJS head-to-head