简体   繁体   English

处理角度承诺中的错误

[英]Handling Errors In Angular Promises

I am still learning promises in angular and have this bit of code where I am making a "GET" request two times. 我仍在学习有角度的promise,并在两次发出“ GET”请求的地方有这段代码。 I want to run one get request before calling the other. 我想在调用另一个之前运行一个get请求。 This is working fine, but how would I handle errors here? 一切正常,但是如何处理错误? If I get an error for my first GET request how do I find out what that error is and prevent my code from calling the second GET request? 如果在第一个GET请求中遇到错误,如何找出错误并阻止代码调用第二个GET请求? Examples with my code would be most helpful. 我的代码示例将最有帮助。

apiServices.login = function(user,password,callback) {
$http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/login/login/?username="+user+"&password="+password+"")
        .then(function(contentResponse){
            resultsObject.content = contentResponse; 
            return $http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/data/list/"); 
        })
        .then(function(dataResponse){
            resultsObject.reports = dataResponse;
            resultsObject.success = 1;
            console.log(resultsObject);

            callback(resultsObject);
            apiServices.useData(resultsObject);
        }); 
}

dummyData.login(username, password, function (dataStatus) {

            if (dataStatus.success = 1) {

                $rootScope.loggedIn = true;
                $rootScope.selectedDashboard = 1; 
            } else {
                console.log("Error");
            }
        });

I would do things slightly different from Lucas, I prefer chaining a catch block( basically it would act like the synchrounous try...catch block we use) rather than adding an error callback function so code would be like: 我会做一些与Lucas稍有不同的事情,我更喜欢链接一个catch块(基本上它的行为就像我们使用的同步try...catch块一样),而不是添加一个错误回调函数,因此代码如下:

return $http.get(url1)
  .then(function(result){
    resultsObject.url1 = result;
    return $http.get(url2);
  }).then(function(result){
    resultsObject.url2 = result;
    return resultsObject;
  }).catch(function(error){
    // handle error.
  });

PS: most of your code is fine, but I am not really sure why you have that callback(resultsObject); PS:您的大多数代码都可以,但是我不确定为什么要使用该callback(resultsObject); , when you are using promises, callbacks are redundant, you could just return the promise chain $http.get... ,当您使用诺言时,回调是多余的,您可以只返回诺言链$http.get...

You can pass a second parameter in the first callback handling. 您可以在第一个回调处理中传递第二个参数。 This will trigger if there's an error in the request, then you can handle it however you want: 如果请求中有错误,这将触发,然后您可以根据需要进行处理:

 $http({
   method: 'GET',
   url: '/someUrl'
 }).then(function successCallback(response) {
     // this callback will be called asynchronously
     // when the response is available
   }, function errorCallback(response) {
     // called asynchronously if an error occurs     
     // or server returns response with an error status.
   });

Or in your coding: 或在您的编码中:

$http.get('/someUrl').then(successCallback, errorCallback);

More information here 更多信息在这里

Your code would look like: 您的代码如下所示:

$http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/login/login/?username="+user+"&password="+password+"")
    .then(function(contentResponse){
        resultsObject.content = contentResponse; 
        return $http.get("http://magainteractive.com/prototypes/cisco-ima-dashboard/cms/web/api/data/list/"); 
    }, function(error){ 
             //HANDLE ERROR HERE
       })
    .then(function(dataResponse){
        resultsObject.reports = dataResponse;
        resultsObject.success = 1;
        console.log(resultsObject);

        callback(resultsObject);
        apiServices.useData(resultsObject);
    }); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM