Skip to content

Commit

Permalink
feat: add optional default error events (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu authored Jan 26, 2021
1 parent 297581d commit 27fd086
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/errorEvents/CoreCommandError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PieceContext } from '@sapphire/pieces';
import { Event } from '../lib/structures/Event';
import { CommandErrorPayload, Events } from '../lib/types/Events';

export class CoreEvent extends Event<Events.CommandError> {
public constructor(context: PieceContext) {
super(context, { event: Events.CommandError });
}

public run(error: Error, context: CommandErrorPayload) {
const { name, path } = context.piece;
this.context.logger.error(`Encountered error on command "${name}" at path "${path}"`, error.stack);
}
}
14 changes: 14 additions & 0 deletions src/errorEvents/CoreEventError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PieceContext } from '@sapphire/pieces';
import { Event } from '../lib/structures/Event';
import { EventErrorPayload, Events } from '../lib/types/Events';

export class CoreEvent extends Event<Events.EventError> {
public constructor(context: PieceContext) {
super(context, { event: Events.EventError });
}

public run(error: Error, context: EventErrorPayload) {
const { name, event, path } = context.piece;
this.context.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error.stack);
}
}
13 changes: 13 additions & 0 deletions src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export interface SapphireClientOptions {
* @default { instance: new Logger(LogLevel.Info) }
*/
logger?: ClientLoggerOptions;

/**
* If Sapphire should load our pre-included error event listeners that log any encountered errors to the [[SapphireClient.logger]] instance
* @since 1.0.0
* @default true
*/
loadDefaultErrorEvents?: boolean;
}

/**
Expand Down Expand Up @@ -199,6 +206,9 @@ export class SapphireClient extends Client {
}

this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info);

Store.injectedContext.logger = this.logger;

this.fetchPrefix = options.fetchPrefix ?? (() => this.options.defaultPrefix ?? null);

for (const plugin of SapphireClient.plugins.values(PluginHook.PreInitialization)) {
Expand All @@ -212,6 +222,8 @@ export class SapphireClient extends Client {
this.events = new EventStore().registerPath(join(__dirname, '..', 'events'));
this.preconditions = new PreconditionStore().registerPath(join(__dirname, '..', 'preconditions'));

if (options.loadDefaultErrorEvents !== false) this.events.registerPath(join(__dirname, '..', 'errorEvents'));

this.stores = new Set();
this.registerStore(this.arguments) //
.registerStore(this.commands)
Expand Down Expand Up @@ -333,5 +345,6 @@ declare module 'discord.js' {
declare module '@sapphire/pieces' {
interface PieceContextExtras {
client: SapphireClient;
logger: ILogger;
}
}

0 comments on commit 27fd086

Please sign in to comment.