javascript - 捕获以某种方式影响对象评估的错误
问题描述
我有这样的代码结构:
class CustomError {
constructor(status, error = null, message = null) {
this.status = status;
this.error = error;
this.message = message;
}
}
const foo = async function() {
throw new CustomError('foo error');
}
const bar = async function() {
try {
foo();
} catch (error) {
if (error instanceof CustomError) {
throw error;
}
throw new CustomError('bar error');
}
}
bar();
这会引发条形错误。
我把它放在if
声明之前:
console.log(error instanceof CustomError, error)
which 记录:
false
CustomError {status: "foo", message: null, error: null}
这似乎是矛盾的。如果它记录error
为CustomError
不应该error instanceof CustomError
返回true
?
解决方案
推荐阅读
- javascript - 如何使用 discord.js 调用 SubCommand 的选项?(v13)
- javascript - 选择颜色后颜色输入不关闭
- spring - 如何从 Springboot REST API 动态发送响应到 WebUI?
- python - 为满足条件的每一行生成随机数
- ruby-on-rails - 使用 Selenium 进行 Docker-compose 测试失败 / Selenium::WebDriver::Error::UnknownError: unknown error: net::ERR_CONNECTION_REFUSED
- vue.js - 在异步 es6 中使用 v-if 加载
- javascript - 如何解决在 Node.js 中抛出错误的问题
- c++ - 如何在 C++ 中部分填充数组
- sql - 如何使用我从查询中保存到字符串而不是 VBA 中的表中的 SQL 语句创建记录集?适用于 tbl 不适用于 qry
- eclipse - 在eclipse中编辑现有目录