Im using react query as data handler. Im new to react native and kinda new to react query.
Right now i dont really understand why the onError callback is not handling my errors. The error callback is fired by the error still goes to the catch handler (that i dont want to have) in the function.
I have the following setup/structure:
Axios in API layer. No try catch. UseMutation setup with mutationFn pointing to the "axios API request". ClickHandler firing a function that doing the mutation.
const mutation = useMutation({
mutationFn: (req: TimeReportRequest) => {
return updateTimeReport(timeReport?.id!, req);
},
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ['timeReports'] });
},
onError: (error) => {
console.log('Im the error and i should not go further');
errorMessageHandler(error);
},
});
const onSubmitTimeReport = () => {
try {
if (!timeReport) return;
const req: TimeReportRequest = {
...
};
mutation.mutate(req);
} catch (error) {
console.log("im will recieve the error. But i shouldn't?. Error is not handled as a normal try-catch")
}
};
The main problem is that the error is not handled correct. If i remove react query and just use the API-layer (axios method) straight with try catch everything works as expected.
What am i missing here?