diff --git a/src/content/docs/v4/reference/conclusion.mdx b/src/content/docs/v4/reference/conclusion.mdx index 017c8189b..c0b7994d2 100644 --- a/src/content/docs/v4/reference/conclusion.mdx +++ b/src/content/docs/v4/reference/conclusion.mdx @@ -2,7 +2,7 @@ title: Conclusion description: Thank you for reading the sern guide sidebar: - order: 9 + order: 10 --- If you reached this far, thank you for reading! diff --git a/src/content/docs/v4/reference/error-handling.mdx b/src/content/docs/v4/reference/error-handling.mdx new file mode 100644 index 000000000..9ea35e36f --- /dev/null +++ b/src/content/docs/v4/reference/error-handling.mdx @@ -0,0 +1,59 @@ +--- +title: Error handling +description: Properly handle unexpected errors. +sidebar: + order: 9 +--- + +Error handling is important in any application, especially one which has a long lifetime of running. + +## Handling errors in command and event modules + +To capture errors, enable the 'error' event in sern's global event manager. + +import { FileTree } from '@astrojs/starlight/components'; + + +- src/ + - commands/ + - events/ + - **error.js** + - index.js + - config.js + - dependencies.d.ts + + + +:::tip + +Don't forget to enable event handling! +```js title='src/config.js' +export const events = "./dist/events" +``` + +::: + + +```js +import { EventType, eventModule } from '@sern/handler' + +export default eventModule({ + type: EventType.Sern, + name: 'error', + execute: (err) => { + console.log('caught', err) + } +}) +``` + +If the error handler is not set, sern's behavior is to crash the application. +This respects [node.js's default behavior](https://nodejs.org/api/events.html#error-events) + + +:::caution + + Be careful about errors thrown IN the error handler. + If this happens, a memory leaks occurs and your bot **will crash**. + +::: +