the_jozika
the_jozika3mo ago

useQuery Error Handling in Custom hook

How am i supposed to handle an error from useQuery in a custom hook? something like this const useZ = (id:someUserGivenId | undefined) => { const y = useQuery(api...., {id as Id<'mytable'> | undefined}) ///some code i really would not want to have in my component with other hooks return z } this throws an error if the id is not valid. and i would love to handle it inside of this hook. is that somehow possible=
17 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Jamal
Jamal3mo ago
GitHub
convex-helpers/packages/convex-helpers/README.md at main · get-con...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
the_jozika
the_jozikaOP3mo ago
uh nice
퍼플
퍼플3mo ago
why don't you try new ConvexError()?
the_jozika
the_jozikaOP3mo ago
thanks for the quick response 🫶
Jamal
Jamal3mo ago
is this what you were looking for?
the_jozika
the_jozikaOP3mo ago
ill get back in 10 min. but i think
Jamal
Jamal3mo ago
Convex error is ment to be thrown on the server side, hes trying to catch the error on the client
퍼플
퍼플3mo ago
hm... doesn't ConvexError also reach the client side?
the_jozika
the_jozikaOP3mo ago
yes but it gets thrown
Jamal
Jamal3mo ago
Yes but normally you would catch it not create it
the_jozika
the_jozikaOP3mo ago
and i dont want to handle the error in a boundary but according to the error do some stuff
퍼플
퍼플3mo ago
Oh I ment throw it in server-side and catch it
Jamal
Jamal3mo ago
Application Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
Jamal
Jamal3mo ago
I prefer using the convex-helpers hook since it gives an easier API for handling convex query results but you can also use this method if you dont want to use the extra package
the_jozika
the_jozikaOP3mo ago
I really love the convex helpers. it really feels like now my chains got thrown away. It did bother me for like 2 hours that i could not do status depending stuff and everything haha. (I love to put everything i need in a component that is a bit more complicated in its own hook) This just makes me love your product even more 🙂
Jamal
Jamal3mo ago
Glad it helps

Did you find this page helpful?