• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Angular 1.6 可能未处理的拒绝

用户头像
it1352
帮助1

问题说明

我有一个使用 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.

正确答案

#1

此问题已通过 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
系列文章
更多 icon
同类精品
更多 icon
继续加载