Does useSWRMutation not catch thrown errors?
Unanswered
Satin posted this in #help-forum
SatinOP
I'm doing something similar to:
What I'm experiencing right now is that the console log is never reached. And if I wrap the trigger in a try-catch, it's actually going to catch. In the catch though, I can see that error is indeed set by useSWRMutation.
But based on https://swr.vercel.app/docs/error-handling#status-code-and-error-object (that uses
and just set the error const, so that I can do this:
async function fetcher() {throw new Error("I expect to be caught be useSWRMutation"};
const {trigger, error} = useSWRMutation(key, fetcher);
await trigger()
console.log(`I expect to be able to reach this code because useSWRMutation caught the error and set the error const: ${error}`)What I'm experiencing right now is that the console log is never reached. And if I wrap the trigger in a try-catch, it's actually going to catch. In the catch though, I can see that error is indeed set by useSWRMutation.
But based on https://swr.vercel.app/docs/error-handling#status-code-and-error-object (that uses
useSWR) I would expect useSWRMutation to catch the error for me, and just set the error const, so that I can do this:
async function fetcher() {throw new Error("I expect to be caught be useSWRMutation"};
const {trigger, error} = useSWRMutation(key, fetcher);
await trigger()
error ? errorToast : successToast // code never reached