我有一个 AngularJS 的代码:
service.doSomething()
.then(function(result) {
//do something with the result
});
在 AngularJS 1.5.9 中,当我在 .then()
部分出现错误时,例如:
service.doSomething()
.then(function(result) {
var x = null;
var y = x.y;
//do something with the result
});
我收到了明确的错误信息:
TypeError: Cannot read property 'y' of null
但是在 1.6 版本中使用相同的代码我得到了一个不同的错误:
Possibly unhandled rejection: {} undefined
我知道这与 this change 有关,通过添加 .catch()
block ,单个解决方案非常简单:
service.doSomething()
.then(function(result) {
var x = null;
var y = x.y;
//do something with the result
})
.catch(console.error);
现在我又得到了我想要的:
TypeError: Cannot read property 'y' of null
但是如何在不在每个地方添加 .catch()
block 的情况下为整个应用程序获得相同的结果(更详细的错误)?
我测试了建议的解决方案以通过添加来禁用它:
$qProvider.errorOnUnhandledRejections(false);
但情况更糟——我在控制台中没有任何东西!错误被吞没在某个地方,根本没有记录。我不确定这是 AngularJS 1.6 还是我的配置有问题。
您是否知道如何从 1.5.9 版“恢复”日志记录行为?
编辑:
添加自定义错误处理程序:
.factory('$exceptionHandler', function($log) {
return function(exception, cause) {
$log.warn(exception, cause);
};
})
一点帮助也没有。在错误处理程序中,我已经收到“已包装”错误。
最佳答案
这已通过 fix($q): Add traceback to unhandled promise rejections -- Commit 316f60f 修复修复包含在 v1.6.1 release 中.
关于javascript - Angular 1.6 中可能未处理的拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41281515/