Skip to content

要激活開發模式,設置 development: true

ts
Bun.serve({
  development: true, 
  fetch(req) {
    throw new Error("woops!");
  },
});

在開發模式下,Bun 會在瀏覽器中使用內置錯誤頁面顯示錯誤。

error 回調

要處理服務器端錯誤,實現 error 處理程序。此函數應在發生錯誤時返回 Response 提供給客戶端。此響應將覆蓋 development 模式下 Bun 的默認錯誤頁面。

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",
      },
    });
  },
});

Bun學習網由www.bunjs.com.cn整理維護