javascript - 如何区分我可以处理的错误和 Promise 中的程序员错误?
问题描述
我对承诺有疑问。我很难区分我想要崩溃的错误类型和我想要处理的错误。
这就是我认为我应该解决这些类型的错误的方式,但我想知道这是否是一种优雅的方式。
PromiseA //I believe that any errors thrown in this function will crash node
.catch(function( {throw custom_err(msg)}))
//Here I can catch any rejected promises which I believe will always be
//operational errors and then I can flag them as such.
//The error is then thrown to the final catch which handles it appropriately
.then(function() { return Function1().catch(throw custom_err(msg)); })
//Now if an error is thrown by Function1 inside a then statement it will
//fall down the chain. It will then be caught by the final catch and it will not be flagged
//as operational. My final catch will throw it and crash node which I desire.
//However if it is operational Function1 will reject the promise and it will be caught by
//the nested catch which then flags it appropriately and throws to the final catch
.catch(finalErrorHandler);
这个解决方案有效吗?我对拒绝与抛出流程的假设是否正确?
我是否正确假设如果我正在使用的库中的函数引发错误(例如Function1()
)它很可能是程序员错误?
这是解决问题的优雅方法吗?我缺少更明显的解决方案吗?
解决方案
推荐阅读
- batch-file - 批处理循环的正确分隔符
- python - 如何在使用 Python API 创建文件后立即从 Google Vault 导出中下载文件?
- c++ - gsl::cstring_span 不支持 constexpr 吗?如果不是,我为什么要使用它?
- elasticsearch - 如何在同一路径的多个嵌套子查询中返回多个内部命中?
- javascript - 在javascript中为粒子添加不透明度?
- javascript - JS 中的函数没有显示任何输出。我也看不到任何错误
- javascript - sleep() await 仅在异步函数中有效
- reactjs - 为什么不直接从 action creator redux 发送到 store?
- android - Dagger2:无法在 WorkManager 中注入依赖项
- php - 模块 pcntl 已在第 0 行的未知中加载 - Apache x Cli