ampp
ampp6mo ago

mutations and try catch errors.

I've been wanting to catch errors from my event system that can run sets of mutations. From what i understood is that mutations wrapped with a try/catch no longer have any benefits of mutations. Basically it will execute/write everything up till the error right? I tested this. in
try {
await saveEventMutation(
} catch (err)
{
await saveEventMutationError(
}
try {
await saveEventMutation(
} catch (err)
{
await saveEventMutationError(
}
both get ran. I only want to log a error of the event. I'm guessing the only way to do this without both functions saving data is to do it via a try catch within a action? Then that forces me to schedule actions via mutations in many cases.
2 Replies
Michal Srb
Michal Srb6mo ago
We have talked internally about subtransactions, which might allow you to do these kinds of patterns. Otherise you can structure your code to do this manually in JS (create a set of "writes" in saveEventMutation and only apply them to ctx.db at its end). The "if it throws no write is made" is not the only benefit of mutations (mainly you get serializability with other concurrent mutations).
ampp
amppOP6mo ago
Yeah, outside of error catching I havent thought of many reasons for sub transactions for other than just easily breaking up mutations. It makes me think a ctx.runMutationHandler(from mutation) or a type of internalHandlerMutation that can only call the sub transaction, or have a function that takes a primary, and backup mutation.