Skip to content

Include cause and extra properties in error log integrations #149

@sparebytes

Description

@sparebytes

For debugging purposes, it is normal to chain errors together with the standardized error cause property. It is also common to assign extra properties to error objects to provide additional context.

My ask is that additional information about failures be included on the Logs page in the dashboard an in the payload to integrations (Webhook, Sentry, etc):

  • Include information about an error's cause (note, that a cause may have a cause, ad infinitum)
  • Include any custom properties that may be assigned to the error object or any of its causes

Here is an example to demonstrate:

import { v } from "convex/values";
import { action } from "./_generated/server";

// Function that may throw an error
async function sendNotification(_userId: string): Promise<void> {
  throw new Error("Network error");
}

export const testNotification = action({
  args: {
    userId: v.string(),
  },
  handler: async (ctx, args) => {
    try {
      await sendNotification(args.userId);
    } catch (cause) {
      // The caught error is wrapped in another to provide more context
      const error = new Error("Problem sending notification", { cause });
      // Errors may be given a custom name
      error.name = "NotificationError";
      // Additional context is assigned to the error
      Object.assign(error, { userId: args.userId });
      // Log the error for demonstration purposes only
      console.error(error);
      throw error;
    }
  },
});

This action creates two logs. The first is the console.error, which has most of what I'm looking for. It doesn't contain any stack trace, but it does have the cause,the extra userId property, and the custom name.

{ [NotificationError: Problem sending notification] [cause]: [Error: Network error], name: 'NotificationError', userId: 'TEST' }

The second log is from the throw error. Here's what we find if we look on the Logs page:

[Request ID: 3146e0f1ff56234a] Server Error
Uncaught NotificationError: Problem sending notification
    at handler (../convex/play.ts:19:12)

As you can see, it has the stack trace and the custom name, but it doesn't have the cause or the userId.

We also see more or less the same thing when looking at the webhook payload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions