-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reading Error responses #112
Comments
You should be able to read the server response from the promise resolver. Is that not possible? |
I.e. |
You're right, the response body is not exposed through I'm just curious about the use case. Because getting a 422 back from an API means there's a programming error. What value does that provide to the application you're building, to be able to read that response from the API? |
@egeriis This is not only a result of programming errors, but also covers problems with user input and form validations, which in this case are done on the server side. In this case, a lot of form validation falls back on the server and with a rather complicated model structure, so it's not always feasible to perform client-side validation to prevent this. Also, JSON API conventions account for error objects being thrown as described at http://jsonapi.org/examples/#error-objects. |
☝️ this is my co-worker by the way. sorry for the late reply. |
@stefanvignir @Realtin You're absolutely right, don't know why I didn't consider that 🙂 It does look like we're exposing the response though: https://github.com/dixieio/redux-json-api/blob/master/src/utils.js#L20 And I think I was a bit quick in my earlier reply, because it looks like all the detail is there, in your screenshot? What are you missing from the body in the response object from your screenshot? |
my first screenshot (in this comment: #112 (comment)) is from the Chrome Dev Tools (Network Section) this is the raw response that is coming in from the backend. And the second one is the console.log in my promise where I log the whole response object that I get through redux-json-api so the errors[i].detail which would be interesting to have access to inside the frontend is missing after the response goes through redux-json-api. |
could it be that the error.body in the Javascript error object is not filled correctly because the errors that come from the backend are an array? btw. I'm happy to try to work on a fix for that if we can pinpoint the problem. |
I am not sure what |
|
The The source pointer then points to the attribute related to the error, to make it possible to highlight the field relating to the error. (Example: |
@stefanvignir Aah! You're referring to the JSON API spec. Ok, so here's the deal: you can read the error response already. As you can see in That means that you can view the response object through You can read more about the response object here: https://developer.mozilla.org/en-US/docs/Web/API/Response |
just to update: I was looking at the so: createEntity(stuff)
.then(
(res) => {
console.log(res)
},
(error) => {
error.response.json().then((response) => {
console.log(response);
});
}); this finally gives me access to the Error Array! @egeriis do you think that would be nice to have in the documentation? I could make a PR on the Weekend for this if you think it would be helpful to others. |
@Realtin It has nothing to do with redux-json-api, it's a regular Response object. But it would definitely be useful to mention that we're passing the Response object to the error 🙂 |
When an API Request fails my Server sends a response with an explanation like this:
if I catch the error from the promise I'll only get
Error: Unprocessable Entry
is there a way I can access the error response somehow?
The text was updated successfully, but these errors were encountered: