Comments (10)
It's because of this https://github.com/ember-cli/ember-ajax/blob/master/addon/services/ajax.js#L320. This was inherited from EmberData Adapter.
It can easily be changed, but I'm curios what should happen instead. When it's an object, should I just include the object in details or serialize it to JSON?
from ember-ajax.
I was just running into the same issue and resolved it for our use case by overriding the ajax service with
normalizeErrorResponse(status, headers, payload) {
if (payload && typeof payload === 'object' && payload.errors) {
return payload.errors;
} else {
return payload;
}
}
Background: if the error payload doesn't follow the { errors: {...}}
convention, I would rather see the raw payload and deal with it on the individual error handler.
from ember-ajax.
Maybe ember-ajax should switch over to what the ember-data adapters are using now? It looks like Its using a much better approach.
handleResponse(status, headers, payload, requestData) {
if (this.isSuccess(status, headers, payload)) {
return payload;
} else if (this.isInvalid(status, headers, payload)) {
return new InvalidError(payload.errors);
}
let errors = this.normalizeErrorResponse(status, headers, payload);
let detailedMessage = this.generatedDetailedMessage(status, headers, payload, requestData);
return new AdapterError(errors, detailedMessage);
},
https://github.com/emberjs/data/blob/master/addon/-private/adapters/rest-adapter.js#L755
from ember-ajax.
In current master, normalizeErrorResponse
thankfully no longer converts the payload to a string, but I think there's another problem:
ember-ajax/addon/ajax-request.js
Lines 171 to 191 in 1cfd942
If the error isn't a generic AjaxError (i.e. an InvalidError, ServerError, etc.), the normalizeErrorResponse
function is never called. Thus if the payload doesn't have an errors
key, there's no way to inspect the response from the server. normalizeErrorResponse
ought to always be called prior to returning an error.
from ember-ajax.
@the83 if you have a chance, could you check out #43 and see if that handles the problem you're running into? That changes ember-ajax
to handle request errors in the same way that ember-data
does. I totally see what you're saying though, the hook should be called first, and then the errors array that that returns should be passed into the other error types. I'd be happy to update #43 to fix that issue.
from ember-ajax.
@alexlafroscia that commit you just added should fix the problem. Thanks!
from ember-ajax.
@the83 awesome 👍
from ember-ajax.
My back end is returning a 500 code with the following response:
{
errorMessage: ".. some description..."
}
In my .catch
block, the error I receive looks like this:
{
"message": "Ajax operation failed",
"name": "Error",
"errors": [
{
"status": "500",
"title": "The backend responded with an error",
"detail": "[object Object]"
}
]
}
It looks like my original response is still being stringified; is this intentional? Is there anything I can do to stop this happening?
from ember-ajax.
@AndyPyeSC can you please try the beta release and let us know if this is still a problem.
from ember-ajax.
@taras Just tried it and it's working, thanks!
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.