useAction error handling
I see. So if my useAction errors though, its not obvious how to return that to the UI unless I am just wrapping in a try catch and returning the error as values?
I was thinking something like this:
6 Replies
Returning the error as a value is a primary approach, yes. The other is throwing a
ConvexError
in your action, if you prefer throwing. Great guide in the docs: https://docs.convex.dev/functions/error-handling/application-errorsApplication Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
Hmm I guess all this would take some getting used to since I've been so comfortable with tRPC and Tanstack router for a while. It still feels a bit like I would be wanting to wrap my convex hooks with a custom hook that would give me some more nuance and expose states / events like "onMutate", "onSuccess" and "onError" for mutations and actions and "isLoading" for queries
Actually wrapping them is fine, I just wouldn’t wrap them in tanstack query. A simple wrapper for custom DX makes sense, you’ll just want to make sure the types flow through.
That makes sense. Thanks so much for your help! Do you know if anyone has made some list of useful wrappers? I'm going to play around with it a bit more for sure
There are examples of this in the convex helpers repo: https://github.com/get-convex/convex-helpers/blob/d0ad873079edc9d87b060b74a3a50bbfb0c49502/packages/convex-helpers/react/sessions.ts#L99
GitHub
convex-helpers/packages/convex-helpers/react/sessions.ts at d0ad873...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
Oh awesome