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•3y ago
error should have what you want: https://tanstack.com/query/latest/docs/react/reference/useQuery
Or better yet: https://tkdodo.eu/blog/react-query-error-handlinguseQuery | TanStack Query Docs
const {
data,
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•3y ago
different error messages in case 403 and 401 response codesreact 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-goldOP•3y ago
Awesome!!! Thanks your answers 👍