mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
41 lines
1021 B
Plaintext
41 lines
1021 B
Plaintext
---
|
|
title: Error Handling
|
|
description: Learn how to handle errors in Bun's development server
|
|
---
|
|
|
|
To activate development mode, set `development: true`.
|
|
|
|
```ts title="server.ts" icon="/icons/typescript.svg"
|
|
Bun.serve({
|
|
development: true, // [!code ++]
|
|
fetch(req) {
|
|
throw new Error("woops!");
|
|
},
|
|
});
|
|
```
|
|
|
|
In development mode, Bun will surface errors in-browser with a built-in error page.
|
|
|
|
<Frame></Frame>
|
|
|
|
### `error` callback
|
|
|
|
To handle server-side errors, implement an `error` handler. This function should return a `Response` to serve to the client when an error occurs. This response will supersede Bun's default error page in `development` mode.
|
|
|
|
```ts
|
|
Bun.serve({
|
|
fetch(req) {
|
|
throw new Error("woops!");
|
|
},
|
|
error(error) {
|
|
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
|
|
headers: {
|
|
"Content-Type": "text/html",
|
|
},
|
|
});
|
|
},
|
|
});
|
|
```
|
|
|
|
<Info>[Learn more about debugging in Bun](/runtime/debugger)</Info>
|