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
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!
@the_jozika Have a look at https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/README.md#richer-usequery
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
uh nice
why don't you try new ConvexError()?
thanks for the quick response 🫶
is this what you were looking for?
ill get back in 10 min. but i think
Convex error is ment to be thrown on the server side, hes trying to catch the error on the client
hm... doesn't ConvexError also reach the client side?
yes
but it gets thrown
Yes but normally you would catch it not create it
and i dont want to handle the error in a boundary
but according to the error do some stuff
Oh I ment throw it in server-side
and catch it
@the_jozika https://docs.convex.dev/functions/error-handling/application-errors#handling-application-errors-on-the-client these are the docs for errors incase you need it also
Application Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
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 packageI 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 🙂
Glad it helps