SamplePack
SamplePack12mo ago

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:
const { data: paymentUrl, isLoading: paymentUrlLoading } = useQuery({
queryKey: ["paymentUrl"],
queryFn: useAction(api.stripe.getPaymentUrl),
});
const { data: paymentUrl, isLoading: paymentUrlLoading } = useQuery({
queryKey: ["paymentUrl"],
queryFn: useAction(api.stripe.getPaymentUrl),
});
6 Replies
erquhart
erquhart12mo ago
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-errors
Application Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
SamplePack
SamplePackOP12mo ago
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
erquhart
erquhart12mo ago
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.
SamplePack
SamplePackOP12mo ago
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
erquhart
erquhart12mo ago
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
SamplePack
SamplePackOP12mo ago
Oh awesome

Did you find this page helpful?