Skip to content

Commit b7103b4

Browse files
committed
export error codes for better event error handling
1 parent 8d97354 commit b7103b4

File tree

5 files changed

+103
-9
lines changed

5 files changed

+103
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
type EventHandler,
3+
stopEvents,
4+
isErrorType,
5+
CommandKitErrorCodes,
6+
} from 'commandkit';
7+
8+
const handler: EventHandler<'messageCreate'> = (message) => {
9+
try {
10+
// code that may throw an error
11+
12+
stopEvents(); // conditionally stop the event chain
13+
} catch (error) {
14+
if (isErrorType(error, CommandKitErrorCodes.StopEvents)) {
15+
console.log('Stopping event chain');
16+
// if stopEvents() is called in the try block, throw it so CommandKit can stop the event chain
17+
throw error;
18+
}
19+
20+
console.log('Not stopping event chain');
21+
// this means that the code threw the error, and stopEvents() was not called
22+
// the rest of the event handlers will be executed as normal
23+
console.error(error);
24+
}
25+
};
26+
27+
export default handler;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
type EventHandler,
3+
stopEvents,
4+
isErrorType,
5+
CommandKitErrorCodes,
6+
} from 'commandkit';
7+
8+
const handler: EventHandler<'messageCreate'> = (message) => {
9+
try {
10+
// code that may throw an error
11+
12+
throw new Error('test');
13+
14+
// stopEvents(); // conditionally stop the event chain
15+
} catch (error) {
16+
if (isErrorType(error, CommandKitErrorCodes.StopEvents)) {
17+
console.log('Stopping event chain');
18+
// if stopEvents() is called in the try block, throw it so CommandKit can stop the event chain
19+
throw error;
20+
}
21+
22+
console.log('Not stopping event chain');
23+
// this means that the code threw the error, and stopEvents() was not called
24+
// the rest of the event handlers will be executed as normal
25+
console.error(error);
26+
}
27+
};
28+
29+
export default handler;
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { Logger } from 'commandkit';
1+
import { type EventHandler, Logger } from 'commandkit';
22

3-
export default function onReady() {
4-
Logger.log('Ready from legacy event handler');
5-
}
3+
export const once = true;
4+
5+
const handler: EventHandler<'ready'> = (client, c, commandkit) => {
6+
Logger.log(`Ready from legacy event handler: ${client.user.username}`);
7+
};
8+
9+
export default handler;

apps/website/docs/guide/03-events/03-stopevents-function.mdx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,54 @@ handlers to control the flow of execution.
1111
## Usage
1212

1313
```ts title="src/app/events/messageCreate/event.ts"
14-
import type { Message } from 'discord.js';
15-
import { stopEvents } from 'commandkit';
14+
import { type EventHandler, stopEvents } from 'commandkit';
1615

17-
export default async function (message: Message) {
16+
const handler: EventHandler<'messageCreate'> = (message) => {
1817
console.log('Message received:', message.content);
1918

2019
// Stop further event handlers of messageCreate from being executed after this point
2120
stopEvents();
22-
// code below will not be executed
23-
}
21+
// code below, and the rest of the event handlers, will not be executed
22+
};
23+
24+
export default handler;
2425
```
2526

2627
:::warning
2728

2829
Calling `stopEvents()` in a try/catch block may lead to unexpected
2930
behavior.
3031

32+
If you still want to use `stopEvents()` in a try/catch block, you can
33+
use the `isErrorType` function to check if the error is an instance of
34+
the `CommandKitErrorCodes.StopEvents` error.
35+
36+
```ts title="src/app/events/messageCreate/event.ts"
37+
import {
38+
type EventHandler,
39+
stopEvents,
40+
isErrorType,
41+
CommandKitErrorCodes,
42+
} from 'commandkit';
43+
44+
const handler: EventHandler<'messageCreate'> = (message) => {
45+
try {
46+
// code that may throw an error
47+
48+
stopEvents(); // conditionally stop the event chain
49+
} catch (error) {
50+
if (isErrorType(error, CommandKitErrorCodes.StopEvents)) {
51+
// if stopEvents() is called in the try block, throw it so CommandKit can stop the event chain
52+
throw error;
53+
}
54+
55+
// this means that the code threw the error, and stopEvents() was not called
56+
// the rest of the event handlers will be executed as normal
57+
console.error(error);
58+
}
59+
};
60+
61+
export default handler;
62+
```
63+
3164
:::

packages/commandkit/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export * from './app/interrupt/signals';
2828
export type { CommandKitHMREvent } from './utils/dev-hooks';
2929
export * from './utils/constants';
3030
export * from './app/events/EventWorkerContext';
31+
export { CommandKitErrorCodes, isErrorType } from './utils/error-codes';
3132
export { Collection, type Client } from 'discord.js';
3233

3334
// cli

0 commit comments

Comments
 (0)