【发布时间】:2019-09-06 09:17:07
【问题描述】:
我正在尝试在我的 NestJS 应用程序中使用异常过滤器。我按照here 的说明设置了我的全局ExceptionFilter,如下所示:
@Catch()
export class DemoExceptionFilter implements ExceptionFilter
{
private readonly logger: Logger;
constructor()
{
this.logger = new Logger(DemoExceptionFilter .name);
}
public catch(exception: unknown, host: ArgumentsHost): void
{
this.logger.log(exception);
}
}
在我的 AppModule 中,我以这种方式注册了 DemoExceptionFilter:
@Module({
...
providers: [
...
{
provide: APP_FILTER,
useClass: DemoExceptionFilter
}
...
]
})
当我在代码中的某处抛出异常时,NestJS 在控制台中记录了异常,但我的 DemoExceptionFilter 没有被调用。
我也试过
app.useGlobalFilters(new DemoExceptionFilter());
在main.ts,这也不起作用。
我错过了什么?
【问题讨论】:
-
你在哪里抛出异常?
-
在服务构造函数中。
标签: javascript node.js typescript nestjs