lincolnloop / amygdala

RESTful HTTP client for JavaScript powered web applications
BSD 3-Clause "New" or "Revised" License
393 stars 22 forks source link

How to catch errors #18

Closed arkadiusz-wieczorek closed 9 years ago

arkadiusz-wieczorek commented 9 years ago

I have deletePlace function in my project. When I send DELETE without authorization to some resource server I've got the error 401, how can I catch errors in amygdala?

this.deletePlace = function(place_id) {
    return store.remove('places', {
        'id': "/" + place_id
    }).done(function() {
        ee.emit('change');
    })
}

Error example caused by lack of authorization

DELETE http://localhost/api/v1/places/qx9143jmj7 401 (Unauthorized)
XHR finished loading: DELETE "http://localhost/api/v1/places/qx9143jmj7"
Uncaught #<XMLHttpRequest>
mlouro commented 9 years ago

@debian-sh you can add an extra callback on done():

this.deletePlace = function(place_id) {
    return store.remove('places', {
        'id': "/" + place_id
    }).done(function() {
        ee.emit('change');
    }, function(xhr) {
       alert('error');
    })
}