T
TanStack2y ago
xenial-black

Handling Error

When i make a request, when api fails i dont receive the api payload from the api that has been sent. Here is the mutation fn
export const verifyEmail = async (token: string) => {
const response = await axiosinstance.post("/v2/auth/verify_email", { token });
return response.data;
};
export const verifyEmail = async (token: string) => {
const response = await axiosinstance.post("/v2/auth/verify_email", { token });
return response.data;
};
but in error i get Request failed with status code 400 this but i want the payload that has been sent from the api... what to do and can someone point me to right direction how to utilize if api sent status code like 400, 500, how to handle error in those situation in react query
4 Replies
conscious-sapphire
conscious-sapphire2y ago
check if error is an instance of AxioError and then grab the response body
xenial-black
xenial-blackOP2y ago
so i have to use try and catch in mutation function itself and get the body and throw the body with in it
export const verifyEmail = async (token: string) => {
try {
const response = await axiosinstance.post("/v2/auth/verify_email", { token });
return response.data;
} catch (err) {
if (err instanceof AxiosError) {
throw new Error(err.response?.data);
}

throw err;
}
};
export const verifyEmail = async (token: string) => {
try {
const response = await axiosinstance.post("/v2/auth/verify_email", { token });
return response.data;
} catch (err) {
if (err instanceof AxiosError) {
throw new Error(err.response?.data);
}

throw err;
}
};
conscious-sapphire
conscious-sapphire2y ago
you can do that, or check the error in the component, that's up to you
xenial-black
xenial-blackOP2y ago
ohh understoood

Did you find this page helpful?