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?
| Moment | Typical error | How you see it |
|---|---|---|
| File loading or one-time startup | InitializationError | SWAG.create() rejects |
| Client-ready feature | FeatureExecutionError | startFeatures() rejects |
| Application command sync | CommandDeploymentError | deployCommands() rejects |
| Command callback | CommandExecutionError | onError or logger |
| Discord send/edit | InteractionResponseError or MessageResponseError | onError or logger |
| Autocomplete callback or response | AutocompleteError | onError or logger |
| Custom event callback | EventExecutionError | onError 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.