DE

Deno Error Handling

Error handling patterns for Deno applications

Details

Language / Topic
denoDeno
Category
Error Handling

Rules

balanced
- Use structured error classes extending `Error` with descriptive messages.
- Use `AbortController` and `AbortSignal` for request timeouts.
- Handle `Deno.serve()` errors via the `onError` callback.
- Return proper HTTP error responses with appropriate status codes.
- Use `globalThis.addEventListener("unhandledrejection")` for unhandled promises.
- Use structured error classes extending `Error` with descriptive messages and optional `cause` for error chaining.
- Use `AbortController` and `AbortSignal` for request timeouts and cancellation of long-running operations.
- Handle `Deno.serve()` errors via the `onError` callback to return custom error responses instead of crashing.
- Return proper HTTP error responses with appropriate status codes (`Response` with `{ status: 400 }`, `{ status: 500 }`, etc.).
- Use `globalThis.addEventListener("unhandledrejection")` to catch and log unhandled promise rejections.
- Use `globalThis.addEventListener("error")` to catch uncaught exceptions and perform cleanup before exit.
- Never swallow errors with empty catch blocks — log the error with context and either rethrow or return a meaningful response.