简体   繁体   中英

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. I want to run one get request before calling the other. 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? 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:

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); , when you are using promises, callbacks are redundant, you could just return the promise chain $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);
    }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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