Comments (8)
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.
Do you mean for detecting the errors? Or specifying a handler function on a per-error-type (401
, 403
, etc) basis?
from ember-ajax.
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.
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.
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.
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.
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.
@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)
- Release notes error? HOT 1
- Is this package deprecated? HOT 7
- allow host and namespace be empty
- Unable to extract error if server return `application/problem+json` header
- Cannot override ajax service HOT 17
- Host does not resolve when the start of the path is the same string as the namespace.
- ember-ajax does not work with Ember 3.8 when transpiling to ES5 HOT 1
- importing `ember-ajax/errors` is broken with `ember-cli-typescript` HOT 4
- Fails with Octane blueprint HOT 6
- Remove requirement that TS consumers must enable allowSyntheticDefaultImports
- Update README to remove unhelpful wording, move contributions
- Request URL
- deprecation warning ember 3.9 - computed-property.volatile HOT 3
- Use with VSCode typechecking in .js files
- Using run.join not working with thrown error
- Any plans to switch to fetch? HOT 7
- Deprecation warnings with Ember 3.27
- [5.1.0] Type-definitions are missing from release HOT 3
- Does this addon work with ember octane versions ? HOT 3
- How to use error handler helpers in glimmer version? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ember-ajax.