Skip to content
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

[api] Return error Response #8

Open
jfsiii opened this issue Mar 14, 2023 · 2 comments
Open

[api] Return error Response #8

jfsiii opened this issue Mar 14, 2023 · 2 comments

Comments

@jfsiii
Copy link
Contributor

jfsiii commented Mar 14, 2023

I want to a) not throw an error when !response.ok b) have full access to the Response (statusText, body, headers) etc

e.g. given

const response = await api.post(`/endpoint`, { bad: 'data' }`

I'd like response to always be a Response regardless of what HTTP status code was returned. Is this possible?

(a) seems to be addressed by handleError but I cannot figure out how to achieve (b). I've tried every combination of handleError, afterFetch, etc I can think of but I can never seem to get the body of an error response back (e.g. if /endpoint returns a 4xx.

Looking at the code, the issue seems to be that .then(handleStatus) is always called. And handleStatus converts that Response to an Error, losing all the info from the Response

  if (!response.ok) {
    throw new Error(response.statusText);
  }

Do I have that correct? Either way, can you suggest any way to achieve what I'm describing?

Edit: I now see that getting the Response would be a bigger change than skipping handleStatus. IIUC, skipping handleStatus would allow access to the processed body though

@thepassle
Copy link
Owner

I think the handleError stuff can be made a plugin as well, I think that makes sense. Would you be willing to make a pr?

@thepassle
Copy link
Owner

Actually, thinking more on this, instead of throwing with only the statustext, cant we just throw with the entire response object?

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

No branches or pull requests

2 participants