A tiny, flexible error reporting helper for Node/Bun apps. Send errors to Discord, Slack, files, or extend with your own providers.
- Send errors to Discord (incoming webhook)
- Send errors to Slack (incoming webhook)
- Persist errors to files (per-ID logs)
- Pluggable: add more send modes (HTTP, Sentry, SMTP, DB, etc.)
This project uses Bun. To install dependencies:
bun installImport and create an instance. send() returns a Promise that resolves with a generated id string that you can reference later.
import ErrorHandler from "./src";
// Discord example
const discord = new ErrorHandler("discord", "https://discord.com/api/webhooks/.....");
discord.send(new Error("Something went wrong")).then(id => {
console.log("Error sent with ID:", id);
});
// Slack example
const slack = new ErrorHandler("slack", "https://hooks.slack.com/services/.....");
slack.send(new Error("Slack test")).then(id => console.log(id));
// File example (writes logs/<id>.log)
const file = new ErrorHandler("file", "logs");
file.send(new Error("File test")).then(id => console.log(id));When sending to Discord/Slack the payload is formatted as:
`<id>`
```text
<raw error text or stack>
## API
- new ErrorHandler(sendMode?: SendMode, sendLogsInfo?: string)
- sendMode: one of `"discord" | "file" | "slack"` (defaults to `"file"`)
- sendLogsInfo: for `file` this is a directory path (default `logs`). For `discord` and `slack` this is the webhook URL.
- send(error: any): Promise<string>
- Sends the provided error and resolves with a generated UUID string (the error id) on success, or rejects with an informative error message on failure.
Notes:
- The `formatError` logic prefers `error.stack` first, then `error.message`, then string conversion. The module intentionally sends the raw error text to remote services.
- The library resolves with the generated id so callers can reference or store this id.
## Extending with more send modes
Add a new `SendMode` and implement a private `sendTo<Mode>` method that accepts `(info: string, id: string, text: string)` and returns `Promise<string>` (resolve with id on success). See `src/index.ts` for existing examples (`sendToDiscord`, `sendToSlack`, `sendToFile`).
Suggested modes to add: `http`, `sentry`, `smtp`, `db`, `aws-sns`, `elasticsearch`, `noop` (testing).
## Testing / Development
Run the example test script that sends an error using the configured mode in `test/index.ts`:
```bash
bun run test/index.ts
The test script prints the returned id or an error message.
- When sending to chat webhooks, long stacks or messages may be truncated by the provider. Consider trimming or summarizing large payloads.
- If the error text contains triple backticks (```), the code block may break; consider escaping or sanitizing backticks before sending.
- For production use, consider adding retries, timeouts, and sensitive-data scrubbing.
Contributions welcome. Open issues or PRs to add new providers, tests, or options.
MIT