Skip to content

CIRCE / DOCUMENTATION

Error boundary

Decide what the user sees when a handler fails or a guard denies.

Documentation menu
On this page

src/error.ts exports defineErrorBoundary. Every hook is optional; the defaults reply ephemerally.

defineErrorBoundary({
  onCommandError?(error, ctx),
  onContextMenuCommandError?(error, ctx),
  onComponentError?(error, ctx),
  onModalError?(error, ctx),

  onGuardDenied?(reason, ctx),
  onContextMenuGuardDenied?(reason, ctx),
  onComponentGuardDenied?(reason, ctx),
  onModalGuardDenied?(reason, ctx),

  onUnknownCommand?, onUnknownContextMenuCommand?, onUnknownComponent?, onUnknownModal?,  // stale interactions
  onError?(error, context: { kind, name, correlationId, timestamp, guildId?, channelId? }),
});
import { defineErrorBoundary } from "@sxnnyside/circe";
import { MessageFlags } from "discord.js";

export default defineErrorBoundary({
  async onCommandError(error, ctx) {
    ctx.logger.error(`/${ctx.interaction.commandName} failed: ${error.message}`, { error });
    const reply = { content: "Something went wrong.", flags: MessageFlags.Ephemeral } as const;
    if (ctx.interaction.deferred || ctx.interaction.replied) await ctx.interaction.followUp(reply);
    else await ctx.interaction.reply(reply);
  },
  onError(error, context) {
    // Sentry, Datadog, your logs…
  },
});

onError

The central telemetry hook. It receives failures from every kind: command, autocomplete, component, modal, context-menu and event. Listener failures are isolated per listener.

This is for runtime failures. Startup failures are in Errors.