Error handling with useQuery
Try/catch makes sense for handling
ConvexError
with mutations, but how does it work with useQuery()
?
The docs mention that it works with query/mutation/action, but there's only an example for mutations: https://docs.convex.dev/functions/error-handling/application-errors#handling-application-errors-on-the-clientApplication Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
2 Replies
That docs page could do with a link to error handling with the React client.
Try/catch doesn't make sense in React components because like an
if
statement is causes hooks to be run conditionally which isn't allowed by the rules of hooks. You can use an error boundary to catch the error in a parent element or write a custom hook to return the error object. That's how useQueries
works, it returns either undefined
for loading, a Convex value, or an error object.Makes sense.
useQuery
style hooks in other libs usually include an optional error object, I can wrap it and make one.