简体   繁体   English

我们可以使用JS中的catch(error)处理http(4XX-5XX)的所有错误情况吗?

[英]Can we handle all the errors scenarios of http (4XX-5XX) using catch(error) in JS?

I am using a promise to access some data from some URL and to catch the errors i want to use .catch . 我正在使用一个承诺,以从一些URL访问一些数据,并捕获我想使用.catch的错误。 If i miss some error i can anyway catch in .finally but what all errors am i ignoring? 如果我错过了一些错误,无论如何我还是可以抓住。但我忽略的所有错误是什么?

 $http.get('/someUrl', config)
      .then(function(data) {      
    }).catch(function activateError(error) {
           if (!error.handled) {
           alert(error);
           }
    }).finally(function(){
    });

The response of the $http call returns an object with a status property. $ http调用的响应返回一个具有status属性的对象。 It's a number corresponding to the response code of your request. 该数字与您的请求的响应代码相对应。

So you could try handling your errors this way: 因此,您可以尝试通过以下方式处理错误:

$http.get(dataUrl)
    .success(function (data){
    })
    .error(function (error, status){
       if (!error.handled) {
           alert(error);
       }
       // handle error treatment with status code
  }); 

Hope it helped! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以用 try catch 块 React 应用程序处理错误(状态代码 4xx)吗 - Can React app handle error (status code 4xx) with try catch block 使用then / catch语法通过axios分别捕获4xx和代码错误 - Catching 4xx and code errors separately with axios using then / catch syntax 可以在控制台中隐藏资源加载的5xx / 4xx错误吗? - Can 5xx/4xx errors for resource loads be hidden in the console? 在JS中将xx:xx转换为xxhxx - Convert xx:xx into xxhxx in JS 在 fetch() Promise 中,当状态为 4xx 或 5xx 时,如何捕获服务器错误消息? - Within a fetch() Promise, how to .catch server errors messages when status is 4xx or 5xx? 使用 Redux 工具包异步 REST ZDB974238714CA8DE6FZA 模式时如何捕获 HTTP 4xx 错误? - How to trap HTTP 4xx errors when using Redux Toolkit async REST API pattern? 是4xx和5xx.network错误吗? - Is 4xx and 5xx network errors? 如何在 fetch api 中处理 HTTP 代码 4xx 响应 - How to handle HTTP code 4xx responses in fetch api Access-Control-Allow-Origin'标头包含多个值'http://xx.xx.xx.xx:3002,http://xx.xx.xx.xx:3002',但只允许一个 - Access-Control-Allow-Origin' header contains multiple values 'http://xx.xx.xx.xx:3002, http://xx.xx.xx.xx:3002', but only one is allowed 当存在4xx或5xx Http错误代码时,在Javascript中解析JSONP响应 - Parsing JSONP Response in Javascript when 4xx or 5xx Http Error Code is Present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM