要激活开发模式,设置 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",
},
});
},
});