T
TanStack3y ago
harsh-harlequin

isError does not come true when onError is raised

I have an usemutation example, when I get 500 in this example, it falls to onErrror, but the isError, error or isSuccess I use does not work correctly. const queryClient = useQueryClient(); return useMutation( ({ id, values }: driverCreateResponse) => createDriver(id, values), { mutationKey: "driverCreate", onSuccess: (data, variables) => { toast.success("Sürücü başarıyla eklendi."); queryClient.invalidateQueries([moving-company-${variables.id}]); }, onError: (error: any) => { toast.error("Sürücü eklenirken bir hata oluştu."); throw new Error(error); }, } const { mutate: createMutate, isError, error } = useCreateDriver();
6 Replies
xenophobic-harlequin
xenophobic-harlequin3y ago
you don't need to throw an error in onError
harsh-harlequin
harsh-harlequinOP3y ago
I did this to try it but it never returns isError true
xenophobic-harlequin
xenophobic-harlequin3y ago
does createDriver return a rejected Promise in case of error?
harsh-harlequin
harsh-harlequinOP3y ago
export const createDriver = async (id: string | undefined, values: any) => api.post(${CONSOLE_MOVING_COMPANIES}/addDriver/${id}, values); my post function export const post = async (location: string, body: any, headers?: any) => { const token = getToken(); return axios .post(BASE_URL + location, body, { headers: { ...headers, ...(token && { Authorization: token }), }, }) .then((response) => response.data) .catch((error) => { if (error.response.status === 403) { localStorage.removeItem("token"); window.location.href = "/"; } throw error.response.data.error.errors[0].detail ? error.response.data.error.errors[0].detail : error.toJSON(); }); }; actually return throw error message
xenophobic-harlequin
xenophobic-harlequin3y ago
then it should work - can't say much more without seeing a reproduction
harsh-harlequin
harsh-harlequinOP3y ago
oky ty

Did you find this page helpful?