Angular 1.6 可能未处理的拒绝
问题说明
我有一个使用 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'
TypeError: Cannot read property 'y' of null
但在使用相同代码的 1.6 版中,我收到了不同的错误:
But in version 1.6 with the same code I'm getting a different error:
可能未处理的拒绝:{} undefined
Possibly unhandled rejection: {} 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'
TypeError: Cannot read property 'y' of null
但是如何在不添加 .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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcbihbh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20