在Angular 1.6中可能无法处理拒绝

编程入门 行业动态 更新时间:2024-10-18 20:23:30
本文介绍了在Angular 1.6中可能无法处理拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有AngularJS的代码:

I have a code with AngularJS:

service.doSomething() .then(function(result) { //do something with the result });

当我在 .then()中出错时,在AngularJS 1.5.9中部分如:

service.doSomething() .then(function(result) { var x = null; var y = x.y; //do something with the result });

我收到明确错误信息:

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):将回溯添加到未处理的承诺拒绝 - 提交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中可能无法处理拒绝

本文发布于:2023-11-14 19:48:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!