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
| Method | Signature | Returns |
|---|---|---|
createConcreteContext | createConcreteContext(metadata: T, contextId: undefined, inquirerId: string) | R |
getFilterInstance | `getFilterInstance(filter: Function | ExceptionFilter, contextId: undefined, inquirerId: string)` |
getInstanceByMetatype | getInstanceByMetatype(metatype: Type<unknown>) | `InstanceWrapper |
reflectCatchExceptions | reflectCatchExceptions(instance: ExceptionFilter) | Type<any>[] |
Properties
| Property | Type |
|---|---|
moduleContext | string |
Where it refuses work
BaseExceptionFilterContextstops the work with an early return whenisEmpty(metadata).BaseExceptionFilterContextstops the work with an early return whenisObject.BaseExceptionFilterContextstops the work with an early return when!instanceWrapper.BaseExceptionFilterContextstops the work with an early return when!this.moduleContext.BaseExceptionFilterContextstops the work with an early return when!moduleRef.
Diagram
mermaidgraph 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
tsimport {
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)
ExceptionFiltersContext—packages/microservices/context/exception-filters-context.ts:16ExceptionFiltersContext—packages/websockets/context/exception-filters-context.ts:10
Was this page helpful?