#Proper way to get mutation status

9 messages · Page 1 of 1 (latest)

digital schooner
#

What is a proper way to handle error handling for both queries and mutation?

peak minnowBOT
#

Hi @digital schooner. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Convex provides different ways to handle errors for both queries and mutations.

For mutations, you can handle errors by using either .catch on your mutation call or try...catch if you're using an async handled function. Here's an example:

sendMessage(newMessageText).catch((error) => {  
  // Do something with `error` here  
});  

// or

try {  
  await sendMessage(newMessageText);  
} catch (error) {  
  // Do something with `error` here  
}  

If you handle your mutation error, it will no longer become an unhandled promise rejection. You may need to report this error to your exception handling service manually (source (https://docs.convex.dev/functions/error-handling/#errors-in-mutations)).

For queries, the error handling process is not explicitly mentioned in the provided sources. However, a user in a Discord thread asked a similar question. The user wanted to know how to handle errors when using useQuery in a React application. Unfortunately, the thread did not provide a clear answer (source (https://discord.com/channels/1019350475847499849/1253581087003775017)).

In general, Convex will automatically handle internal Convex errors. If there are problems on their end, they'll automatically retry your queries and mutations until the problem is resolved and your queries and mutations succeed. However, you must decide how to handle application, developer, and read/write limit errors. When one of these errors happens, the best practices are to show the user some appropriate UI, send the error to an exception reporting service, and log the incident (source (https://docs.convex.dev/functions/error-handling/)).

#

@digital schooner kapa.ai is still learning and improving, please let us know how it did by reacting below

digital schooner
#

Is it possible to check when a query/mutation has begun/completed?

#

@peak minnow Is it possible to check when a query/mutation has begun/completed?

peak minnowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, it is possible to check when a query/mutation has begun or completed in Convex.

For queries, you can check if a query is loading by checking if the result is undefined. Here's an example:

const result = useQuery(...);
const isLoading = result === undefined;

This indicates that the query is still loading if the result is undefined (source (https://discord.com/channels/1019350475847499849/1227707978824552449)).

For mutations, you can track the status of a mutation via the promise returned by the mutation function. When you call a mutation function, it returns a promise. This promise will resolve when the mutation has been committed and the results are showing up in queries. Here's an example:

const sendMessage = useMutation(api.myModule.sendMessage);

sendMessage(newMessageText).then(() => {
  // The mutation has completed
}).catch((error) => {
  // An error occurred
});

In this example, the then block will execute when the mutation has completed, and the catch block will execute if an error occurred (source (https://discord.com/channels/1019350475847499849/1227707978824552449)).

Please note that these methods provide a basic way to check the status of queries and mutations. Depending on your specific use case, you might need to implement more complex logic.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: