Code Monkey home page Code Monkey logo

Comments (8)

alexlafroscia avatar alexlafroscia commented on July 30, 2024 2

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.

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

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

from ember-ajax.

caselas avatar caselas commented on July 30, 2024

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

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

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!

from ember-ajax.

taras avatar taras commented on July 30, 2024

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

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

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

from ember-ajax.

urbany avatar urbany commented on July 30, 2024

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.

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

@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!

from ember-ajax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.