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

Can't acess to useFetch response's body on error ? #526

Closed
NoBrainSkull opened this issue May 20, 2021 · 2 comments · Fixed by #572
Closed

Can't acess to useFetch response's body on error ? #526

NoBrainSkull opened this issue May 20, 2021 · 2 comments · Fixed by #572

Comments

@NoBrainSkull
Copy link

On a HTTP 400 response sent by the server (missing required fields in a form), useFetch only pass the statusText to the Error object. source code

I'd like to access to the json body response where I can access to the details of the error (statusText is irrelevant in my case). Response's body is already consumed (Stream locked) and data object has not been populated since the error is thrown before it gets the chance.

Is this some kind of non-fetch-standard issue and should I use useAxios instead ?

@wheatjs
Copy link
Member

wheatjs commented May 24, 2021

I'll look into fixing this, thanks for the report

@schelmo
Copy link
Contributor

schelmo commented Jun 2, 2021

also stumbled upon this (using the 5 beta)
i'm getting a 422 response with a json encoded response
the body is already consumed when trying to get it from the response object
maybe change:

if (!fetchResponse.ok)
  throw new Error(fetchResponse.message)

to something like this:

if (!fetchResponse.ok) {
  const error = new Error(fetchResponse.statusText)
  error.data = responseData
  throw error
}

then we could consume it via:

onFetchError((e) => console.log(e.message, e.data)

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