T
TanStack3y ago
like-gold

How to not show Axios errors in console?

Is it possible to not show axios errors in console while using React Query? I have simple code:
const fetchData = async () => {
const result = await axios.get("/api/user")
return result.data
}

export const authUserQuery = () => ({
queryKey: ["authUser"],
queryFn: async () => {
const data = await fetchData()
return data
},
})
const fetchData = async () => {
const result = await axios.get("/api/user")
return result.data
}

export const authUserQuery = () => ({
queryKey: ["authUser"],
queryFn: async () => {
const data = await fetchData()
return data
},
})
When user is not authenticated, server responses with 401. When I try to get data using
const user = useQuery(authUserQuery().queryKey, authUserQuery().queryFn)
const user = useQuery(authUserQuery().queryKey, authUserQuery().queryFn)
It tries 4 times with console.log GET * 401 (Unauthorized) and then finishing with AxiosError {message: 'Request failed with status code 401' I expect to see more suitable error in console, but not axios one.
6 Replies
like-gold
like-goldOP3y ago
Also is my code is good enought or it can be improved?
rival-black
rival-black3y ago
1) you only see those in dev mode, not production 2) we have removed logging failed queries in v5 3) in v4, you can use a custom logger to turn off error logging, but you'll have to remove the custom logger in v5 (because of point 2)
rival-black
rival-black3y ago
Custom Logger | TanStack Query Docs
If you want to change how information is logged by TanStack Query, you can set a custom logger when creating a QueryClient. `tsx
like-gold
like-goldOP3y ago
Thanks for help! So I see initial error from query in dev mode just for information purposes right? I thought I did smth wrong with catching exceptions. Is it better to use v4 or v5 will not have any major changes and it is already safe to use for production purposes?
rival-black
rival-black3y ago
we're going to ship a release candidate soon; I don't expect any major api changes anymore
like-gold
like-goldOP3y ago
thanks!

Did you find this page helpful?