Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Overwrite error method? #57

Closed
caselas opened this issue Feb 19, 2016 · 8 comments
Closed

Overwrite error method? #57

caselas opened this issue Feb 19, 2016 · 8 comments

Comments

@caselas
Copy link

caselas commented Feb 19, 2016

I see an example for "globally" handling errors when the status is 200, but what about other status codes like 401 or 403?

I would think something like this would be possible:

// app/services/ajax.js

import AjaxService from 'ember-ajax/services/ajax';

export default AjaxService.extend({
  isUnauthorized(status, headers, payload ) {
    // some custom logic
  }
});

Rather than handling specific errors on each request, is it possible to handle them globally?

@alexlafroscia
Copy link
Collaborator

Do you mean for detecting the errors? Or specifying a handler function on a per-error-type (401, 403, etc) basis?

@alexlafroscia
Copy link
Collaborator

Right now, the way to do that would probably be overwriting the

import AjaxService from 'ember-ajax/services/ajax';
import { isUnauthorizedError } from 'ember-ajax/errors';

export default AjaxService.extend({
  handleResponse() {
    const result = this._super(...arguments);
    if (isUnauthorizedError(result)) {
      // Do whatever you need to do
    } else {
      return result;
    }
  }
});

So instead of providing specific hooks (which could become kind of messy, given a bunch of possible errors) you can use the single handleResponse hook, check the return type of the original handler, detect the type of error using the provided checking functions (details on isUnauthorizedError can be found here) and then do your custom handling however you want to.

Alternatively, if you want to change how an "unauthorized error" is detected (and maybe try some other logic before deciding to throw an error) you can overwrite the isUnauthorizedError method on the service, which is defined here and used here.

@caselas
Copy link
Author

caselas commented Feb 19, 2016

The former is exactly what I was looking for, just didn't realize I had to import the error. (Still getting the hang of es6.)

Thanks @alexlafroscia

@caselas caselas closed this as completed Feb 19, 2016
@alexlafroscia
Copy link
Collaborator

No problem! You could also do a simple type check of the result variable, like:

if (result instanceof UnauthorizedError) {
  // ...
}

But then you need to import UnauthorizedError instead. We provide the helper methods for doing this exact kind of thing -- I'm glad that works for you!

@taras
Copy link
Contributor

taras commented Feb 19, 2016

I think we should make helpers the recommended way to tests errors because
it will allow us to also check for Ember Data errors.

Taras Mankovski

@alexlafroscia
Copy link
Collaborator

Yeah, for sure. I was more just pointing out that the UnauthorizedError is a unique type that can be checked that way, too

@urbany
Copy link

urbany commented Jun 17, 2016

Hi @alexlafroscia this handleResponse method is very interesting, it would be great if you could add it to the README, this exact example is very helpful.

@alexlafroscia
Copy link
Collaborator

@urbany Thanks! @taras and I are planning to put together a little documentation site with examples and stuff of these kinds of things, since I want to avoid saturating the README with too much information. I'll make sure that that example ends up in there!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants