node.js - 抛出 HTTP 嵌套异常会导致 Promise 被拒绝
问题描述
我正在尝试抛出一个 http 嵌套异常以使用默认的嵌套处理程序。
我试图在调用 adminService.update 函数的控制器中抛出异常,并且非常成功。
async update(update: DeepPartial<Admin>) {
const admin = await this.findOne({ id: update.id});
const adminName = await this.findOne({ username: update.username});
if (!adminName) {
throw new ConflictException('Username already in use');
}
admin.username = update.username;
admin.save();
}
将调用放入控制器时的输出:
{
"statusCode": 409,
"error": "Conflict",
"message": "Username already in use"
}
控制器方法。
@Put()
async update(@Body() updateDTO: UpdateDTO): Promise<void> {
throw new ConflictException('Username already in use');
this.adminService.update(updateDTO);
}
错误本身:
UnhandledPromiseRejectionWarning: Error: [object Object] at AdminService.<anonymous> (C:\Users\JBRETAS_EXT\Documents\mapa-digital\dist\admin\admin.service.js:55:19) at Generator.next (<anonymous>)
解决方案
似乎我的控制器方法中缺少 return 语句。
@Put()
async update(@Body() updateDTO: UpdateDTO): Promise<void> {
return this.adminService.update(updateDTO);
}
推荐阅读
- c - C lang strlen 函数返回错误值
- android - Flutter 位置包帮助:我想将用户证明的坐标提取到单独的 var/string 或 double 中
- video - ffmpeg 分布式转码
- julia - 从压缩 Q 中恢复 Q(spqr,以稀疏方式)
- r - 通过 R 中的 barplot() 函数在分组的 barplot 中绘制误差线
- swift - 为什么我可以创建静态变量的实例?
- css - 样式化组件关键帧动画不适用于 React 组件
- java - javap结果中的重复方法
- javascript - 将表情符号插入 Angular 的输入字段
- php - 如何在 laravel 中使用 ajax 成功插入数据库?