erquhart
erquhart17mo ago

Data doesn't commit when re-throwing error

Just checking - is there any reason a convex validation error wouldn't get caught by a try/catch? I'm seeing an insert fail, and the validation error is correct, but the whole thing is wrapped in a try/catch, but the catch isn't happening. Happening in an internal mutation in the convex runtime, function triggered via cli.
4 Replies
ballingt
ballingt17mo ago
Where is the try/catch? Is this a Convex action calling a mutation? when you say "the whole thing is wrapped in a try/catch" I'm not sure what that means.
erquhart
erquhartOP17mo ago
Almost the entire body of the mutation is within a try/catch, but that wasn't actually helpful info - main point is that the insert that's failing is inside of a try/catch, and I'm awaiting it. When it throws, the error is logged in convex logs, but the catch doesn't execute. Basically:
export const paginate = internalMutation({
args: {
migrationId: v.id('migrations'),
},
handler: async ({ db, scheduler }, { migrationId }) => {
try {
await db.insert('my_table', { some: 'data' }
} catch (e) {
// this never runs
await db.patch(migrationId, { status: 'error', error: e?.toString() })
throw e
}
}
})
export const paginate = internalMutation({
args: {
migrationId: v.id('migrations'),
},
handler: async ({ db, scheduler }, { migrationId }) => {
try {
await db.insert('my_table', { some: 'data' }
} catch (e) {
// this never runs
await db.patch(migrationId, { status: 'error', error: e?.toString() })
throw e
}
}
})
The only thing in the console when it fails is the error. I was thinking it wasn't catching, but looking at it now it seems more likely that the log is from the rethrow. But the patch doesn't happen, which I would expect to cause a separate error before the rethrow. Maybe the patch is failing silently?
ballingt
ballingt17mo ago
Ah sorry for the delay — if a mutation throws an error, that aborts then whole transaction! If you catch it this is fine, but if you re-throw it the entire transaction is rolled back.
erquhart
erquhartOP17mo ago
Ah, right, it’s a transaction 🤦‍♂️🤦‍♂️ Thanks!

Did you find this page helpful?