SWAGcommands
Guides

Errors and reporting

Understand framework failures and handle them in one place.

SWAGcommands wraps framework failures in SwagError subclasses. They carry a stable code, a phase, a context object, and usually the original cause. Add onError for runtime reporting:

const swag = await SWAG.create({
  client,
  commandsDir: "./dist/commands",
  onError: async (error, context) => {
    console.error(error.code, error.phase, context, error.cause);
  },
});

Callback, Discord response, autocomplete, event, precondition, and recurring-feature failures go through this hook. Without it, SWAGcommands logs the error. Keep the hook robust: if the reporter itself fails, some event and feature paths can only log that secondary error.

What fails where?

MomentTypical errorHow you see it
File loading or one-time startupInitializationErrorSWAG.create() rejects
Client-ready featureFeatureExecutionErrorstartFeatures() rejects
Application command syncCommandDeploymentErrordeployCommands() rejects
Command callbackCommandExecutionErroronError or logger
Discord send/editInteractionResponseError or MessageResponseErroronError or logger
Autocomplete callback or responseAutocompleteErroronError or logger
Custom event callbackEventExecutionErroronError or logger

Precondition failures are normal denials, not necessarily exceptions. Use onPreconditionFailure to decide what the user sees. A thrown precondition or failure hook becomes PreconditionExecutionError and goes to onError.

Catch deployment explicitly

import { CommandDeploymentError } from "swagcommands";

try {
  await swag.deployCommands();
} catch (error) {
  if (error instanceof CommandDeploymentError) {
    console.error("Failed target:", error.context);
    console.error("Already synced:", error.completedTargets);
  }
  throw error;
}

Deployment spans independent targets and does not roll earlier targets back if a later one fails. The error catalog lists the exported classes and where they come from.

On this page