Javascript 全局错误筛选器不捕获来自服务的异常,只捕获控制器
我有一个api NestJS,在这里我实现了一个错误过滤器来捕获所有类型的异常。在main.ts中,我将api设置为全局使用过滤器 显然,它只捕获控制器中的错误,因为当我在服务上下文中抛出异常时,异常会在控制台中抛出,api会下降 main.ts:Javascript 全局错误筛选器不捕获来自服务的异常,只捕获控制器,javascript,exception,nestjs,Javascript,Exception,Nestjs,我有一个api NestJS,在这里我实现了一个错误过滤器来捕获所有类型的异常。在main.ts中,我将api设置为全局使用过滤器 显然,它只捕获控制器中的错误,因为当我在服务上下文中抛出异常时,异常会在控制台中抛出,api会下降 main.ts: import { NestFactory } from '@nestjs/core'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { Fast
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './exception-filters/http-exception.filter';
import { AllExceptionsFilter } from './exception-filters/exception.filter';
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter());
const options = new DocumentBuilder()
.setTitle('API Agendamento.Vip')
// .setDescription('')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
// app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new AllExceptionsFilter());
await app.listen(process.env.PORT || 3001);
}
bootstrap();
我的异常过滤器:
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, BadRequestException } from '@nestjs/common';
import { FastifyRequest, FastifyReply } from 'fastify';
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response: FastifyReply<any> = ctx.getResponse();
const request: FastifyRequest = ctx.getRequest();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const objResponse = Object.assign(exception, {
timestamp: new Date().toISOString(),
path: request.req.url
});
response.status(status).send({
objResponse
});
}
}
从'@nestjs/common'导入{ExceptionFilter,Catch,ArgumentsHost,HttpException,HttpStatus,BadRequestException};
从“fastify”导入{FastifyRequest,FastifyReply};
@捕获()
导出类AllExceptionFilter实现ExceptionFilter{
捕获(异常:未知,主机:ArgumentsHost){
const ctx=host.switchToHttp();
常量响应:fastfyReply=ctx.getResponse();
const-request:fastfyrequest=ctx.getRequest();
常量状态=
HttpException的异常实例
?异常。getStatus()
:HttpStatus.INTERNAL\u SERVER\u错误;
const objResponse=Object.assign(异常{
时间戳:新日期().toISOString(),
路径:request.req.url
});
响应。状态(status)。发送({
objResponse
});
}
}
在节点控制台上引发异常,导致api停止
只有当它们在控制器的上下文中时,才会被过滤器捕获
我该怎么做才能在服务中捕获异常呢?看到您的过滤器后,我们可以尝试两件事,首先将HttpException添加到@Catch decorator(仅用于测试,我相信不会有任何区别) 然后,在应用模块中添加以下内容,而不是使用app.useGlobalFilters:
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { AllExceptionsFilter } from './exception-filters/exception.filter';
@Module({
providers: [
{
provide: APP_FILTER,
useClass: AllExceptionFilter,
},
],
})
export class AppModule {}
这将使过滤器处于全局范围,并使您能够使用全局注入器,这是我一直用于全局(过滤器、防护装置、管道)的方法,对我来说非常有用。问题是我在模型方法中抛出异常:
async signInApp(dataLogin: DataLoginDto) {
return new Promise((resolve, _reject) => {
this.userModel.findOne({
email: dataLogin.email
}, (err, user) => {
if (err) { throw new InternalServerErrorException('error', err); }
if (!user) { throw new NotFoundException('device info missing'); }
// ...Code omitted
我将异常放在mongoose方法的范围之外,从而更改了代码:
async signInApp(dataLogin: DataLoginDto) {
const user = await this.userModel.findOne({
email: dataLogin.email
}).select('password active').exec();
if (!user) { throw new NotFoundException('user not found'); }
if (!user.active) { throw new UnauthorizedException('unable to access your account'); }
// ...Code omitted
你能显示你的异常过滤器吗?@RalphJS,我编辑并包含了异常过滤器。你正在使用的三个异常是HttpExceptions,即使在catch decorator上添加它也不起作用。你也可以尝试使用APP_filter将过滤器添加到全局范围。我将添加一个示例智能地不起作用。当异常在服务上引发时,服务会在节点控制台上持续引发异常。使用此选项,您将无法控制内部服务器错误。我的aproach几乎相同,但在express上,它的工作方式很有魅力。即使我将MongoError添加到@Catch(),nestjs的logger和Fastfy可能会有一些行为吗?
async signInApp(dataLogin: DataLoginDto) {
return new Promise((resolve, _reject) => {
this.userModel.findOne({
email: dataLogin.email
}, (err, user) => {
if (err) { throw new InternalServerErrorException('error', err); }
if (!user) { throw new NotFoundException('device info missing'); }
// ...Code omitted
async signInApp(dataLogin: DataLoginDto) {
const user = await this.userModel.findOne({
email: dataLogin.email
}).select('password active').exec();
if (!user) { throw new NotFoundException('user not found'); }
if (!user.active) { throw new UnauthorizedException('unable to access your account'); }
// ...Code omitted