我对诺言有疑问。对于我来说,很难区分要崩溃的错误类型和要处理的错误。
这就是我认为应该解决这类错误的方式,但我想知道这是否是一种优雅的方式。
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()
),则很可能是程序员错误?
这是解决问题的一种优雅方法吗?我还缺少更明显的解决方案吗?