lassiterL
Convex Community2mo ago
2 replies
lassiter

error handling with custom mutation and queries

Advice🎁Feature Request
I keep getting type errors in the different approaches I've taken from building out types from the CustomBuilder type in convex-helpers to just leaving it plain. I tried using the kagi ai a few times but didn't really get an answer that worked. Right now setting up a HOC around handler seems the easiest way to wrap to handle error telemetry but am running into a few issues. Overall, I'm wanting to emit events when handlers throw exceptions in custom queires and mutations built with convex-helpers.

Current Setup

We're using convex-helpers customMutation/customQuery with an onSuccess callback that emits wide events on the success path:
  export const authedMutation = customMutation(mutation, {
    args: traceArgsValidator,
    input: async (ctx, args) => {
      const startTime = Date.now();
      const event = createWideEventBuilder({ /* ... */ });

      return {
        ctx: { auth, authz, event, _startTime: startTime },
        args: handlerArgs,
        onSuccess: () => {
          // :white_check_mark: This works great for success path
          event.set("function.status", "success");
          emitWideEvent(event);
        },
      };
    },
  });

The Problem

There's no onError callback in customMutation/customQuery, so we tried wrapping handlers:
  export const log = authedMutation({
    args: { entityType: v.union(...), entityId: v.string() },
    handler: withErrorEvent(async (ctx, args) => {
      // We want ctx and args to remain fully typed here
      await ctx.db.insert("activityLog", { entityType: args.entityType });
    }),
  });
Was this page helpful?