[英]Can React app handle error (status code 4xx) with try catch block
[英]Can we handle all the errors scenarios of http (4XX-5XX) using catch(error) in JS?
我正在使用一个承诺,以从一些URL访问一些数据,并捕获我想使用.catch的错误。 如果我错过了一些错误,无论如何我还是可以抓住。但我忽略的所有错误是什么?
$http.get('/someUrl', config)
.then(function(data) {
}).catch(function activateError(error) {
if (!error.handled) {
alert(error);
}
}).finally(function(){
});
$ http调用的响应返回一个具有status
属性的对象。 该数字与您的请求的响应代码相对应。
因此,您可以尝试通过以下方式处理错误:
$http.get(dataUrl)
.success(function (data){
})
.error(function (error, status){
if (!error.handled) {
alert(error);
}
// handle error treatment with status code
});
希望能有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.