Skip to main content
要启用开发模式,设置 development: true
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79server.ts
Bun.serve({
  development: true, 
  fetch(req) {
    throw new Error("woops!");
  },
});
在开发模式下,Bun 会通过内置的错误页面在浏览器中显示错误。
Bun 内置的 500 错误页面

error 回调

要处理服务器端错误,请实现一个 error 处理函数。当发生错误时,该函数应返回一个 Response 供客户端使用。此响应会覆盖开发模式下 Bun 默认的错误页面。
Bun.serve({
  fetch(req) {
    throw new Error("woops!");
  },
  error(error) {
    return new Response(`<pre>${error}\n${error.stack}</pre>`, {
      headers: {
        "Content-Type": "text/html",
      },
    });
  },
});