Search code examples
javascriptangularjspromiseangular-promiseangular-http

Error handling in AngularJS http get then construct


How can I handle an HTTP error, e.g. 500, when using the AngularJS "http get then" construct (promises)?

$http.get(url).then(
    function(response) {
        console.log('get',response)
    }
)

Problem is, for any non 200 HTTP response, the inner function is not called.


Solution

  • You need to add an additional parameter:

    $http.get(url).then(
        function(response) {
            console.log('get',response)
        },
        function(data) {
            // Handle error here
        })