T
TanStack3y ago
stormy-gold

Getting http response code status in useQuery?

Hi there 👋 I'm new in react/react-query I've just wanted to ask if is it possible to access the http response code with useQuery? I'm using useQuery to fetch data from an api and I wanted to display different error messages in case 403 and 401 response codes? thanks!
3 Replies
tame-yellow
tame-yellow3y ago
React Query Error Handling
After covering the sunshine cases of data fetching, it's time to look at situations where things don't go as planned and "Something went wrong..."
old-apricot
old-apricot3y ago
different error messages in case 403 and 401 response codes
react query is quite agnostic to that, it's not bound to data fetching at all. So it depends what your queryFn does: The content of the rejected promise returned from it will be available as error returned from useQuery. If you just do: throw new Error("failed to fetch"), well, then error will only have that message property and nothing more. So it's on you and your data fetching solution to return the proper error with all the information you want during rendering.
stormy-gold
stormy-goldOP3y ago
Awesome!!! Thanks your answers 👍

Did you find this page helpful?