#Data doesn't commit when re-throwing error

9 messages · Page 1 of 1 (latest)

ember bridge
#

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.

onyx burrow
#

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.

ember bridge
#

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
    }
  }
})
#

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?

onyx burrow
#

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.

ember bridge
#

Ah, right, it’s a transaction 🤦‍♂️🤦‍♂️

#

Thanks!