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.
- Custom error responses to clients (not Cloudflare's generic error page)
- Source-mapped stack traces in our Tail Worker for alerting
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) |
{
"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.
{
"outcome": "ok",
"exceptions": [],
"logs": []
}No exception data available to the Tail Worker.
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?
- Cloudflare Workers with Hono
upload_source_maps: truein wrangler config- Tail Worker attached via
tail_consumers
Cloudflare:
- Tail Handler - TailEvent structure and handler API
- Tail Workers - How to set up tail consumers
- Source maps and stack traces - Enabling source map uploads
- Errors and exceptions - Error handling in Workers
Hono:
- HTTPException - Custom error responses with HTTPException