When creating the queryClient
I want to create a global onError
handler that refreshes my access token when the error response code is 401. But I don't know how the status code is accessible on the returned error in the onError
handler.
Below is my global onError
handler and I only need to access the response code in the if statement to refresh my token at the appropriate time.
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: async (error, query) => {
// How to get status code fo error
if (error.status === 401) {
console.log("Refreshing Token");
await api.get("/api/refresh-token");
queryClient.refetchQueries(query.queryKey);
}
},
}),
});