Global Error Catcher
I have an application that has many mutations and these mutations return an error if the user is not logged in. In that case I want to direct user to the login page, however, I do not want to modify all my client code to do this for a particular error. Is there a way to catch all errors on the client side (NextJS app router)?
15 Replies
I believe you're looking for this: https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary
Component – React
The library for web and native user interfaces
But I think what you actually want to do for authentication is something like this: https://docs.convex.dev/auth/clerk#logged-in-and-logged-out-views
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
Thanks
@giray Any luck with this? I need this exact flow but the links above didn't work.
The call to mutation logs an error in the console from convex. I want to redirect when this happens. Error boundary didn't catch this
I
throw new ConvexError("Unauthenticated")
from convex. How do I catch this in one place on the client so I can redirect?The above links do work for me. An error boundary will catch any uncaught errors thrown from child components. It will not catch errors thrown from the same component that renders the error boundary, or obviously any errors thrown by parent components.
i haven't tried this, but i think the documentation says that error boundaries only catch rendering errors, not errors from event handlers and async code, which mutations (usually?) are. https://legacy.reactjs.org/docs/error-boundaries.html#how-about-event-handlers
you can catch a mutation's error with a
Error Boundaries – React
A JavaScript library for building user interfaces
if you don't want to put a try-catch at each call-site, you could make a wrapper
Gah, I thought this might be the case but the docs didn't confirm it, legacy docs ftw
Thank you @lee
That wrapper solution looks like what I needed.
@lee I had a challenge with the wrapper approach and Typescript typings.
This is how I use mutations (Image 1)
When I introduce the wrapper (Image 2)
The function is meant to be reusable, I updated it to match the original convex
useMutation
You're not calling the mutation - try
m(...args).catch
oh yeah sorry i put the args in the wrong place. Thanks @erquhart
Thank you @erquhart @lee