1

Currently I've got this:

$http({
    method: 'POST',
    url: 'http://api-endpoint/somescript/',
    data: formData,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
.then(function (response) {
    console.log(response);
});

If the script on the other end works out ok, then gets called. However, let's say the script on the server end has some sort of error that's not properly caught. If I make up an error by tossing in some garbage call like asdfasdf(), the then function isn't called, and instead I get this in the browser console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://api-endpoint/somescript/. (Reason: CORS
header 'Access-Control-Allow-Origin' missing).

How do I catch this in the Angular code so I can handle it in a user-friendly manner?

EDIT: This is not a duplicate, as it is specific to Angular.

0

2 Answers 2

1

The $q.then() method accepts three function parameters, the first being the handler for a successful callbacks, the second being for errors, and the third being for notify. $http leverages $q behind the scenes so this thenable should behave like the documentation suggests.

To handle errors in the above code, just toss in an additional function for error handling as such:

.then(function (response) {
    console.log(response);
}, function(response) {
    console.log(response);
});

Feel free to browse the $q documentation, specifically The Promise API, for additional details.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It seemed wrong to add all that interceptor code for something so simple.
@CaptSaltyJack No problem, glad this helped and I hope it can help others as well :)
@CaptSaltyJack - Well, I do not agree with you. It will look more wrong if you have 100s of API's and you have to write the second function every where. So, generic handling is the best thing that we can do our code. Though, in your case you had only 1 API as of now in which you want to do error handling, so this second function solution fits well.
1

To handle this, we will have to add a service/factory to intercept http calls. You can do it like this in your config

$httpProvider.interceptors.push('httpRequestInterceptor'); 

Now, the above service will be something like this

angular.module("app").factory('httpRequestInterceptor', function ($q) {

            return {
                'request': function (config) {

                    // Extract request information
                    return config || $q.when(config);
                },
                'responseError': function(rejection) {

                    // Extract response error information and handle errors
                    return $q.reject(rejection);
                 }
             }
        });

7 Comments

@CaptSaltyJack - Did you checked? Will it work for you?
Wow, that's a lot of work for something so simple. I don't know if this will work for me. I need the error handling to be within a specific controller, because the error handling needs to be specific to a certain page.
Within specific controller will mean specific route/state too?
Correct, a specific route and controller.
You can place a check in the factory for your route and if you are using ui-router then state will be the best thing to use
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.