Jcampuza
Jcampuza2mo ago

Hey guys, trying out convex recently and

Hey guys, trying out convex recently and its going well but had a q - does anybody know what the best way would be to get clean error messages from queries? I see in the docs everywhere on how to handle application errors in mutations but it doesn't address queries very well. I know to wrap in react with an error boundary, but still the message field is pretty convuluted including convex specific info and request id. Thats useful for me for logging but I don't want to show that to my users, just the clean client friendly error message. I currently throw the errors from my queries. Maybe I should instead be returning an actual error object instead of throwing, or perhaps throwing a ConvexError instead? Been looking but can't find anywhere documenting best practices for query error handling.
5 Replies
erquhart
erquhart2mo ago
Throwing a ConvexError makes the error readable for your client, otherwise it's an obfuscated error for security. If you do that, error boundaries give you a way to catch and communicate. If you want more control, you can try the richer useQuery helper which provides a more TanStack Query style api, and provides errors in the errors key instead of throwing: 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
Jcampuza
JcampuzaOP2mo ago
That would work, thanks. Is there a method convex team recommends/uses themselves? I.E. prefering a union type for returning errors vs throwing actual errors? Seems like a part of the docs that is missing regarding query specific application errors.
erquhart
erquhart2mo ago
There really isn't one recommended way - the idea is Convex should work very well with various approaches to this sort of thing, and in my experience it generally does. I will say that throwing errors is a lot less work than defining and returning them, but sometimes a mix of the two works, eg., throwing from anywhere inside a larger Convex codebase and catching at the top level a function, then returning that in a typed error response. It really depends on the errors you're dealing with, you may have different approaches depending on where the error is coming from. If you really want to get control of your errors and have a prescriptive approach for everything, there's an Effect library for Convex called Confect: https://github.com/rjdellecese/confect
erquhart
erquhart2mo ago
Convex CEO just tweeted about it recently: https://x.com/jamwt/status/1937871932915110121
Jamie Turner (@jamwt)
Love to see the interest in Effect and @convex_dev happening right now. There is indeed a shared FP fanfic vibe. For those wanting to try them together, Convex super-developer RJ Dellecese has been building with both for years: https://t.co/3I6Bm97KqK
X
Jcampuza
JcampuzaOP2mo ago
Oh nice I may look into that, haven't used effect yet but have been interested in trying it

Did you find this page helpful?