首页 > 解决方案 > 如何区分我可以处理的错误和 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())它很可能是程序员错误?

这是解决问题的优雅方法吗?我缺少更明显的解决方案吗?

标签: javascriptnode.jserror-handlingpromise

解决方案


推荐阅读