Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Last active January 10, 2026 00:48
Show Gist options
  • Select an option

  • Save martinklepsch/a2fb8746b4a9dce6a9b41b1da859fcae to your computer and use it in GitHub Desktop.

Select an option

Save martinklepsch/a2fb8746b4a9dce6a9b41b1da859fcae to your computer and use it in GitHub Desktop.
Cloudflare Tail Worker source-mapped stack traces investigation

Source-mapped stack traces in Tail Workers require unhandled exceptions

Summary

We're building error monitoring with a Tail Worker and want source-mapped stack traces. We found that only unhandled exceptions populate the exceptions array with source-mapped stacks - meaning you can't have both a custom error response AND source-mapped stacks in the Tail Worker.

What we want

  1. Custom error responses to clients (not Cloudflare's generic error page)
  2. Source-mapped stack traces in our Tail Worker for alerting

What we found

With Hono's onError handler:

app.onError((err) => {
  if (err instanceof HTTPException) {
    return err.getResponse(); // Custom response for expected errors
  }
  throw err; // Re-throw for source-mapped stacks
});
Scenario Client Response Tail outcome Tail exceptions Source-mapped?
Re-throw error Cloudflare error page "exception" ✅ Has error ✅ Yes
Return custom response Custom JSON "ok" ❌ Empty N/A
console.error(e) Custom JSON "ok" ❌ Empty ❌ No (just string in logs)

Tail Worker event with unhandled exception

{
  "outcome": "exception",
  "exceptions": [{
    "name": "Error",
    "message": "Test error",
    "stack": "    at triggerTestError (src/worker/index.ts:33:9)\n    at src/worker/index.ts:43:3"
  }]
}

The stack is correctly source-mapped to original TypeScript files.

Tail Worker event when error is caught

{
  "outcome": "ok",
  "exceptions": [],
  "logs": []
}

No exception data available to the Tail Worker.

Question

Is there a way to return a custom error response to the client while still having the exception (with source-mapped stack) appear in the Tail Worker's exceptions array?

Environment

  • Cloudflare Workers with Hono
  • upload_source_maps: true in wrangler config
  • Tail Worker attached via tail_consumers

Documentation consulted

Cloudflare:

Hono:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment