Error handling in AngularJS http get then construct

Federico Elles picture Federico Elles · Jun 13, 2013 · Viewed 107.6k times · Source

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.

Answer

laurent picture laurent · Jun 13, 2013

You need to add an additional parameter:

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