javascript - 使用打字稿装饰器的错误处理无法按预期工作
问题描述
我研究了在打字稿中使用装饰器的错误处理,发现了一个我没想到的行为
function handleError(): any {
return function (_target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
if (typeof original !== 'function') return;
try {
const result = original.apply(this, arguments);
return result;
} catch (e) {
console.error(`An error occurred!\nFunction: ${propertyKey}\nError: ${e}`);
}
};
}
class test {
@handleError()
mightThrow() {
throw 'Oh no, an error occurred!';
}
}
new test().mightThrow();
运行此代码时,我得到以下控制台输出:
An error occurred!
Function: mightThrow
Error: Oh no, an error occurred!
/home/venny/Projects/lol/index.js:28
throw 'Oh no, an error occurred!';
^
Oh no, an error occurred!
(Use `node --trace-uncaught ...` to show where the exception was thrown)
即使我在装饰器函数中发现了错误,为什么我仍然在这里得到标准的 nodejs 未捕获异常控制台输出?
解决方案
推荐阅读
- python - Pandas——聚合多个同名的列?
- android - 如何在Android中更新两个相同的片段
- laravel - 验证多个字段在 Laravel 中是否唯一
- reactjs - React App 无法在 AWS Amplify 上正确重定向
- c# - 为 C# 进行浮点除法的代码在哪里?
- database - MongoDB 获取旧数据,是由于缓存还是不正确使用异步/等待?
- chart.js - 怎么去掉上边框
- apache-kafka - 如何在 Prometheus 中使用多个冗余导出器
- r - ggplot2 r改变背景颜色
- apache - 如何让服务器端包含在安装在 Centros 7 上的 Apache 上工作?