AngularJS - 承诺重新抛出捕获的异常 [英] AngularJS - Promises rethrow caught exceptions

查看:32
本文介绍了AngularJS - 承诺重新抛出捕获的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,$q promise 的catch 函数捕获了异常:

In the following code, an exception is caught by the catch function of the $q promise:

// Fiddle - http://jsfiddle.net/EFpn8/6/
f1().then(function(data) {
        console.log("success 1: "+data)
        return f2();
    })
    .then(function(data) {console.log("success 2: "+data)})
    .catch(function(data) {console.log("error: "+data)});

function f1() {
    var deferred = $q.defer();
    // An exception thrown here is not caught in catch
    // throw "err";
    deferred.resolve("done f1");        
    return deferred.promise;
}

function f2() {
    var deferred = $q.defer();
    // An exception thrown here is handled properly
    throw "err";
    deferred.resolve("done f2");        
    return deferred.promise;
}  

但是,当我查看控制台日志输出时,我看到以下内容:

However when I look in the console log output I see the following:

异常在Angular中被捕获,但也被浏览器的错误处理捕获.这种行为确实会在 Q 库中重现.

The exception was caught in Angular, but was also caught by the error handling of the browser. This behavior does reproduce with Q library.

这是一个错误吗?我怎样才能真正用 $q 捕获异常?

Is it a bug? How can I truly catch an exception with $q?

推荐答案

已在 AngularJS 1.6 版中修复

这种行为的原因是未捕获的错误与常规拒绝不同,因为例如,它可能是由编程错误引起的.在实践中,这结果令人困惑或者对用户来说是不受欢迎的,因为本地承诺和任何其他流行的承诺库都没有将抛出的错误与常规拒绝区分开来.(注意:虽然这种行为不违反 Promises/A+ 规范,但也没有规定.)

Fixed with AngularJS version 1.6

The reasoning for this behavior was that an uncaught error is different than a regular rejection, as it can be caused by a programming error, for example. In practice, this turned out to be confusing or undesirable for users, since neither native promises nor any other popular promise library distinguishes thrown errors from regular rejections. (Note: While this behavior does not go against the Promises/A+ spec, it is not prescribed either.)

由于 e13eea,从承诺的 onFulfilled 中抛出错误onRejection 处理程序被视为与常规拒绝完全相同.以前,它也会被传递给 $exceptionHandler()(除了以错误为理由拒绝承诺).

$q:

Due to e13eea, an error thrown from a promise's onFulfilled or onRejection handlers is treated exactly the same as a regular rejection. Previously, it would also be passed to the $exceptionHandler() (in addition to rejecting the promise with the error as reason).

新行为适用于所有依赖 $q 的服务/控制器/过滤器等(包括内置服务,例如 $http$路线).例如,$http 的 transformRequest/Response 函数或路由的 redirectTo 函数以及路由解析对象中指定的函数,将不再导致调用 $exceptionHandler() 如果他们抛出错误.除此之外,一切都将继续以同样的方式运行;即承诺将被拒绝,路由转换将被取消,$routeChangeError 事件将被广播等.

The new behavior applies to all services/controllers/filters etc that rely on $q (including built-in services, such as $http and $route). For example, $http's transformRequest/Response functions or a route's redirectTo function as well as functions specified in a route's resolve object, will no longer result in a call to $exceptionHandler() if they throw an error. Other than that, everything will continue to behave in the same way; i.e. the promises will be rejected, route transition will be cancelled, $routeChangeError events will be broadcasted etc.

-- AngularJS 开发人员指南 - 从 V1 迁移.5 到 V1.6 - $q

这篇关于AngularJS - 承诺重新抛出捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆