Retrieving body json when errors occur with useQuery
When my server responds with an error, it sometimes passes in an error message that is useful to the user. I want to pass that through to my React code but can't seem to do it, because I need to
throw new Error
and I'm unable to call the await response.json()
in this GET function or else the useQuery
stops working, because it expects the promise as the return value. If I throw the error and then modify the error after the fact with the response body, my react code doesnt update. I'm in a pickle.
my fetch code:
my query
my react code
but I want error.message to be the server response in some cases. Any advice?5 Replies
exotic-emerald•14mo ago
not sure I understand the problem. Can you explain why this wouldn't work ?
?
optimistic-goldOP•14mo ago
const {data,error} = useQuery(queryOptions)
console.log(111, data, error)
this shows all those undefined/null values.
the final line, after all the react is done rendering, is in the fetch call for the code you gave me: console.log(json)

optimistic-goldOP•14mo ago
For some reason, the error is created and thrown too late in the render lifecycle
Maybe I just don't throw and handle errors in data :Think:
exotic-emerald•14mo ago
there are 3 retries per default ... when data is undefined and error is null, the query is likely just in pending state
optimistic-goldOP•14mo ago
ah it is pending, my bad!
I had a
enabled:
on the queryOptions that was computed to false