> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 错误处理

> 了解如何在 Bun 的开发服务器中处理错误

要激活开发模式，设置 `development: true`。

```ts title="server.ts" icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
Bun.serve({
  development: true, // [!code ++]
  fetch(req) {
    throw new Error("哎呀!");
  },
});
```

在开发模式下，Bun 会在浏览器中通过内置错误页面显示错误。

<Frame>
  <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/exception_page.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=d5640ca9a5241c34172ed9b8bada9d60" alt="Bun 内置的 500 页面" width="800" height="579" data-path="images/exception_page.png" />
</Frame>

### `error` 回调

要处理服务器端错误，实现一个 `error` 处理器。当发生错误时，返回一个 `Response` 提供给客户端。在 `development` 模式下，此响应会替换 Bun 的默认错误页面。

```ts theme={null}
Bun.serve({
  fetch(req) {
    throw new Error("哎呀!");
  },
  error(error) {
    return new Response(`<pre>${error}\n${error.stack}</pre>`, {
      headers: {
        "Content-Type": "text/html",
      },
    });
  },
});
```

<Info>[了解有关在 Bun 中调试的更多信息](/runtime/debugger)</Info>
