hasanaktasTR
hasanaktasTR•10mo ago

Catching thrown errors from queries

I reviewed the documents and posts in the support channel, but I did not get the answer I wanted. This client blows up the entire application when I return a throw error from a query. Error bound has been suggested, but what I want is not to kill a component or the entire tree, but just print the error message in toast. I don't want to break the UI. For example, we can catch an error related to onError in the config we give to the provider with the Tanstack query. It is somewhat easy to log the error message with try catch for mutations, but I could not find a way for queries. How can I achieve this? I showed the sample model in the picture.
No description
3 Replies
erquhart
erquhart•10mo ago
There may be a more direct way to achieve what you're trying right now, but it does feel like it would potentially lead to some awkward constructs. Is it reasonable to consider not throwing from queries? Mutations feel more likely to lend themselves to a thrown error as the user is executing a backend action, and you may want to throw from deep in the stack to stop a transaction from committing, for example. But queries could reliably try/catch any issues on the backend and return an error object to the requester without throwing. This also aligns with tanstack query's model pretty closely.
hasanaktasTR
hasanaktasTROP•10mo ago
@erquhart As you said, I can create a general response class and send something like error and data into it. But this time my flexibility will decrease. Writing such a helper function and using it in queries makes my work much easier. I manage the auth side myself. When I send an invalid session-id, the whole application crashes. It wouldn't be bad if ConvexProvider was a structure where I could catch errors 🙂 Do you have any other solution suggestions?
No description
erquhart
erquhart•10mo ago
You can also write a custom query function that wraps Convex's query on the backend - it can validate your session id and return a uniform error message if it's invalid. Paired with a custom useQuery, you can have this behavior run on every query without writing extra code per query, or per caller. Here's a good article covering the basics of customizing these functions: https://stack.convex.dev/custom-functions
Customizing serverless functions without middleware
Re-use code and centralize request handler definitions with discoverability and type safety and without the indirection of middleware or nesting of wr...

Did you find this page helpful?