Skip to content

Commit

Permalink
Merge pull request #76 from sern-handler/error-handling-ref
Browse files Browse the repository at this point in the history
errrefguide
  • Loading branch information
jacoobes authored Jan 13, 2025
2 parents 21f7072 + daabc51 commit d5cee30
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/content/docs/v4/reference/conclusion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
59 changes: 59 additions & 0 deletions src/content/docs/v4/reference/error-handling.mdx
Original file line number Diff line number Diff line change
@@ -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';

<FileTree>
- src/
- commands/
- events/
- **error.js**
- index.js
- config.js
- dependencies.d.ts
</FileTree>


:::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**.

:::

0 comments on commit d5cee30

Please sign in to comment.