javascript - Angular 1.6 中可能未处理的拒绝

标签 javascript angularjs angular-promise angular-providers angularjs-1.6

我有一个 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);
  };
})

一点帮助也没有。在错误处理程序中,我已经收到“已包装”错误。

最佳答案

关于javascript - Angular 1.6 中可能未处理的拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41281515/

相关文章:

javascript - 带有传递参数的 Angular Promise 链接

javascript - 具有标记和数据层行为的 Google map 鼠标悬停

java - SSE 与 angularJS 和 java in play 框架

javascript - Angular js日期过滤器 "yyyy-mm-dd hh:mm:ss"到 "MMM d, yyyy h mm"

json - 如何将 Angular 中的单个 json 数据 "values"绑定(bind)到命名 $scope 变量(使用 SPARQL 查询返回的数据)?

javascript - AngularJS 为什么在一个 promise 上使用 $q.all() ?

javascript - $q.all 在服务中返回 Controller 中的空对象

javascript - 如何修复 IE 上的 innerHTML 不使用 HTML5 doctype 渲染实体的问题?

javascript - 仅允许使用复选框网格选择矩形

javascript - 无法在react.js应用程序中的onClick上保存数据 - 附有codepen