javascript - NestJs - 使用自定义过滤器和 @Catch(MongoError) 捕获 MongoDB 错误
问题描述
我正在使用 NestJS 使用 MongoDB 创建自定义 API。我有以下设置:
// users.controller.ts
@Post('sign-up')
@UseFilters(MongoExceptionFilter)
async signUp(@Body() createUserDto: CreateUserDto): Promise<any> {
return await this.userService.signUp(createUserDto).catch(error => {
throw new BadRequestException(error);
});
}
// user.service.ts
async signUp(createUserDto: CreateUserDto): Promise<User> {
const createUser = new this.userModel(createUserDto);
return await createUser.save();
}
// mongo-exception.filter.ts
import { ArgumentsHost,Catch, ConflictException, ExceptionFilter } from '@nestjs/common';
import { MongoError } from 'mongodb';
@Catch(MongoError)
export class MongoExceptionFilter implements ExceptionFilter {
catch(exception: MongoError, host: ArgumentsHost) {
console.log('>>>>>>>>>>>>>>>>>>>> exception: ', exception);
}
}
// package.json
"dependencies": {
"@nestjs/common": "^5.4.0",
"@nestjs/core": "^5.4.0",
"@nestjs/jwt": "^0.2.1",
"@nestjs/mongoose": "^5.2.2",
"@nestjs/passport": "^5.1.0",
"@nestjs/typeorm": "^5.2.2",
"fancy-log": "^1.3.3",
"mongoose": "^5.4.7",
"nestjs-config": "^1.3.0",
"passport": "^0.4.0",
"passport-http-bearer": "^1.0.1",
"passport-jwt": "^4.0.0",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.2.2",
"typeorm": "^0.2.12",
"typescript": "^3.0.1",
"util": "^0.11.1"
},
现在,每当我对 /sign-up 路由进行 POST 调用时,save()
都应该在user.service.ts
. 这一切都有效。接下来,当我再次发布 /sign-up 路由时,它应该触发 MongoDB 错误,因为具有相同电子邮件地址的用户已经存在(电子邮件地址是唯一的,因此密钥重复)。我只是.catch(err => ...);
在MongoExceptionFilter
. 我不会触发 MongoError。当我@Catch()
留空时,它会触发但无法处理异常。
我究竟做错了什么?自从我看到这篇文章并将其用作基础以来,我似乎无法让它发挥作用。这是 Mongoose 或 NestJS 的更新,为什么它不再起作用了?
解决方案
这是因为您将所有错误转换为BadRequestExceptions
您MongoExceptionFilter
将不承担任何责任;它检查instanceof MongoError
哪个为 a 返回 false BadRequestException
:
return await this.userService.signUp(createUserDto).catch(error => {
throw new BadRequestException(error);
});
这发生在异常过滤器运行之前;他们总是跑到最后。
.catch()
从您的控制器中删除。如果您真的想将所有其他异常转换为 BadRequestExceptions (400),那么您可以编写第二个异常过滤器,它处理 MongoExceptionFilter 未处理的所有异常:
@Catch()
export class BadRequestFilter implements ExceptionFilter {
catch(exception: Error, host: ArgumentsHost) {
const response = host.switchToHttp().getResponse();
response.status(400).json({message: exception.message});
}
}
和
@Catch(MongoError)
export class MongoFilter implements ExceptionFilter {
catch(exception: MongoError, host: ArgumentsHost) {
const response = host.switchToHttp().getResponse();
if (exception.code === 11000) {
response.status(400).json({ message: 'User already exists.' });
} else {
response.status(500).json({ message: 'Internal error.' });
}
}
}
然后将两者都添加到您的控制器中(顺序很重要!):
@UseFilters(BadRequestFilter, MongoFilter)
async signUp(@Body() createUserDto: CreateUserDto): Promise<any> {
推荐阅读
- azure - MS Graph Daemon App - 获取承载令牌
- r - 在R中使用带有自定义函数的apply删除for循环
- django-rest-framework - 使用相同查找应用多个值时如何执行“或”?
- select - 过滤依赖于另一个值的值
- c# - 向 webservice 传递参数并接收值
- highcharts - 可以像这样设置 HIGHCHARTS 样式吗?
- go - 为什么比赛检测器在这里报告比赛条件?
- azure - 在 Azure 上发布特定的 appsettings.json 文件
- reactjs - React 似乎在修改变量名,然后抱怨它们不存在
- java - 我可以知道后台用户是否忘记保存他的更改吗?