workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

Show stacktrace instead of 1xxx error page in wrangler dev

Open Electroid opened this issue 3 years ago • 1 comments
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.

Electroid avatar Feb 11 '22 06:02 Electroid

This would only work for module style workers ofc.

threepointone avatar Feb 11 '22 09:02 threepointone

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

penalosa avatar May 19 '23 18:05 penalosa