burnstony#1975
burnstony#1975•11mo ago

handling error from mutation in client

how best to handle an error during a mutation on the front end I want to display an error when a duplicate entry fails to be added
10 Replies
ballingt
ballingt•11mo ago
Either
myMutation(args).catch((e) => { ... })
myMutation(args).catch((e) => { ... })
or
try {
result = await myMutation(args);
} catch (e) {
// do something with e
}
try {
result = await myMutation(args);
} catch (e) {
// do something with e
}
The thing to do is probably set some state If it's a ConvexError you can count on e.data being what you passed into the ConvexError on the server.
FleetAdmiralJakob 🗕 🗗 🗙
the errors I throw (ConvexError and Error) will not be accessible on the client in prod? So I can't use them to transport data? So I have to use return "<blah blah blah>" to transport data?
lee
lee•11mo ago
ConvexError is accessible on the client in prod
FleetAdmiralJakob 🗕 🗗 🗙
Oh, ok, I thought this because of this line here
No description
lee
lee•11mo ago
that line demonstrates that a ConvexError and its data are accessible on the client
ballingt
ballingt•11mo ago
in this case the data payload was of type { message: string } but it could just as well be { flavor: string, servings: number }
burnstony#1975
burnstony#1975OP•11mo ago
@ballingt what do you mean by set some state? where?
ballingt
ballingt•11mo ago
say a const [mutationError, setMutationError] = useState(); then use that to render something different
burnstony#1975
burnstony#1975OP•11mo ago
Thank you that makes sense

Did you find this page helpful?