workers-sdk
workers-sdk copied to clipboard
Show stacktrace instead of 1xxx error page in wrangler dev
trafficstars
If a script throws an uncaught exception, an ambiguous 1xxx HTML page is rendered. This happens in wrangler dev too. Unless.. in dev mode, we wrap the script in a try/catch handler, and print out the stacktrace. Here is an example:
import worker from "./path/to/entrypoint"
export default {
async fetch(request, env, ctx) {
try {
return await worker.fetch(request, env, ctx)
} catch (err) {
return new Response(err.stack, { status: 500 })
}
}
}
Miniflare uses youch to render the stacktrace, which could also be used to create a pretty output.
This would only work for module style workers ofc.
This is fixed by the move to local mode by default in Wrangler v3 (Miniflare's pretty error pages are shown). Worth noting that it also works for service workers 🎉 thanks to @cameron-robey's middleware system