问题描述
我有一个使用 AngularJS 的代码:
I have a code with AngularJS:
service.doSomething()
.then(function(result) {
//do something with the result
});
在 AngularJS 1.5.9 中,当我在 .then()
部分出现错误时,例如:
In AngularJS 1.5.9 when I have error in the .then()
section like:
service.doSomething()
.then(function(result) {
var x = null;
var y = x.y;
//do something with the result
});
我收到了明确的错误消息:
I'm getting clear error message:
TypeError: 无法读取 null 的属性 'y'
但在使用相同代码的 1.6 版中,我收到了不同的错误:
But in version 1.6 with the same code I'm getting a different error:
可能未处理的拒绝:{} undefined
我知道这与此更改有关,以及单一解决方案通过添加 .catch()
块非常简单:
I know that this is related to this change, and the single solution is quite simple by adding .catch()
block:
service.doSomething()
.then(function(result) {
var x = null;
var y = x.y;
//do something with the result
})
.catch(console.error);
现在我又得到了我想要的东西:
Now I again have what I want:
TypeError: 无法读取 null 的属性 'y'
但是如何在不添加 .catch()
块的情况下为整个应用程序获得相同的结果(更详细的错误)?
But how to obtain the same result (more detailed error) for entire application without adding .catch()
block in every single place?
我通过添加以下内容测试了建议的解决方案以禁用此功能:
I tested the suggested solution to disable this by adding:
$qProvider.errorOnUnhandledRejections(false);
但是这样情况更糟 - 我在控制台中没有任何东西!该错误在某处被吞并根本没有记录.我不确定是 AngularJS 1.6 还是我的配置有问题.
But with this the situation is even worse - I do not have ANYTHING in the console! The error is swallowed somewhere and not logged at all. I'm not sure is it a problem with AngularJS 1.6 or with my configuration.
您对如何从 1.5.9 版恢复"日志记录行为有任何想法吗?
Do you have any ideas how to "restore" logging behavior from version 1.5.9?
添加自定义错误处理程序:
Adding custom error handler:
.factory('$exceptionHandler', function($log) {
return function(exception, cause) {
$log.warn(exception, cause);
};
})
根本没有帮助.在错误处理程序中,我已经收到包装"错误.
does not help at all. In the error handler I already receive the "wrapped" error.
推荐答案
此问题已通过 fix($q): 向未处理的承诺拒绝添加回溯 -- Commit 316f60f 并且修复包含在 v1.6.1 发布.
This has been fixed with fix($q): Add traceback to unhandled promise rejections -- Commit 316f60f and the fix is included in the v1.6.1 release.
这篇关于Angular 1.6 中可能未处理的拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!