swr icon indicating copy to clipboard operation
swr copied to clipboard

SWR global error handling doesn't catch all error

Open vikyw89 opened this issue 1 year ago • 1 comments

Bug report

as per doc here, the error handler should catch all error for global error handling purpose https://swr.vercel.app/docs/error-handling

<SWRConfig value={{
  onError: (error, key) => {
    if (error.status !== 403 && error.status !== 404) {
      // We can send the error to Sentry,
      // or show a notification UI.
    }
  }
}}>
  <MyApp />
</SWRConfig>

But it doesn't register error from:

  • useSWRMutation
  • mutate
  • useSWRImmutable (I'm not sure about this one yet)

Expected Behavior

it should catch all error for global error handling purpose, especially all async function error / fetchers

Repro Steps / Code Example

  • set up useSWRImmutable, useSWRMutation, and throw error inside it
  • check if the error is logged by <SWRConfigValue={{ onError }}>

Additional Context

SWR version. Add any other context about the problem here.

vikyw89 avatar May 23 '23 08:05 vikyw89

workaround for now, catch the error using middleware

vikyw89 avatar May 23 '23 08:05 vikyw89