ic-ajax icon indicating copy to clipboard operation
ic-ajax copied to clipboard

error handling in an ember-cli environment?

Open 1updesign opened this issue 11 years ago • 3 comments

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.

1updesign avatar Nov 10 '14 16:11 1updesign

(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.

johnnyshields avatar Nov 16 '14 17:11 johnnyshields

@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 avatar Dec 09 '14 20:12 ninjatronic

@ninjatronic nice one, I've raised PR #31 to document this in the readme.

johnnyshields avatar Dec 10 '14 04:12 johnnyshields