error handling in an ember-cli environment?
Hey,
Apologies if this is answered elsewhere, I haven't managed to find it.
What's the best way to me to handle errors from ic-ajax in an ember-cli environment? I'm specifically trying to log my user out of ember-simple-auth after a 401 response from the server.
I'd like to define it globally if possible.
Is there a canonical way to do this? At the moment I'm using a mixin method in the promise fail callback...
Many Thanks.
(Ignore previous comment, I've deleted it to avoid confusion.)
You can do it with ic-ajax.
The error handler function is the second arg of the promise .then() call.
Example:
ajax("my/path", {
data: data
}).then(function(response) {
return console.log(response.message);
}, function(error) {
return doSomething(error.jqXHR.responseJSON);
});
Note that to get the jQuery response object for the error you call the .jqXHR method.
@1updesign @johnnyshields - try this...
ApplicationRoute = Em.Route.extend
init: ->
@_super()
Em.$(document).ajaxError (evt, jqxhr) ->
console.log evt
# your error handling here
`export default ApplicationRoute`
@ninjatronic nice one, I've raised PR #31 to document this in the readme.