gpt4 book ai didi

javascript - angularjs $http.get() 调用上的 catch() 不会抑制错误

转载 作者:行者123 更新时间:2023-12-01 01:14:42 24 4
gpt4 key购买 nike

我有以下代码(为了示例而简化和running in codepen):

var app = angular.module("httptest", []);

app.controller("getjson", ["$scope", "$http", function($scope, $http) {
$http.get("https://codepen.io/anon/pen/LVEwdw.js").
then((response) => {
console.log(response.data)
console.log('in then')
throw 'the error'
}).catch((e) => {
console.log('in the catch')
console.log(e);
});
}]);

我的期望是,如果输入 catch() block ,错误将不会显示在控制台中,除非明确记录(即,它不会显示为红色) 。然而,情况并非如此,红色错误消息打印到控制台,然后进入 catch() block 。我尝试设置an equivalent example here它不使用 AngularJS 的 $http,它的行为符合我的预期:

var promise1 = new Promise(function(resolve, reject) {
resolve();
});

promise1.then((result) => {
console.log('about to throw error')
throw 'hey'
}).catch(function(error) {
console.log(error);
});

在此示例中,没有红色错误通过。

这里发生了什么,是否可以抑制 $http 情况下处理的错误?

最佳答案

随着 AngularJS 1.6 的发布,该行为已得到修复:

来自 GitHub 提交:

fix($q): treat thrown errors as regular rejections

Previously, errors thrown in a promise's onFulfilled or onRejected handlers were treated in a slightly different manner than regular rejections: They were passed to the $exceptionHandler() (in addition to being converted to rejections).

AngularJS GitHub Commit e12eea

另请参阅AngularJS Developer Guide - Migrating to V1.6 - $q

关于javascript - angularjs $http.get() 调用上的 catch() 不会抑制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54876924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com