shiichan

Say Goodbye to Vanishing Stack Traces in Workers Logs!

Hello, it's Shiichan! Today I found an update that makes debugging Workers a bit easier, so let me tell you about it!

Cloudflare Changelog developers.cloudflare.com

What was announced?

On the Cloudflare Changelog, they announced that console methods in Workers now preserve exception details. When your Worker logs an exception, the log entry now includes the exception's name, message, and stack.

The story so far

Until now, when you passed an exception object straight into something like console.error(), the stack trace wasn't always properly preserved, so you'd know an error happened but have a hard time tracking down where.

What changes

For example, if you catch and log an exception like this,

try {
  throw new Error("Deliberately created exception");
} catch (error) {
  console.error("caught exception:", error);
}
  • If you use Workers Observability, your dashboard log is automatically enriched with structured error information
  • The stack trace now appears directly in the log message
  • If you send telemetry to a Tail Worker, the log entry there also carries the detailed exception info

Dive Deep

Log entries delivered to a Tail Worker now include a new errorInfo array.

{
  "message": ["Request failed:", "RangeError: Value out of range"],
  "errorInfo": [
    null,
    {
      "name": "RangeError",
      "message": "Value out of range",
      "stack": "RangeError: Value out of range\n    at ..."
    }
  ],
  "level": "error",
  "timestamp": 1784851200000
}

Each item in errorInfo corresponds to the console argument at the same index in message. Arguments that aren't exceptions get a null entry. So even if you pass multiple arguments, like console.error("caught exception:", error), you can tell exactly which one was the exception.

Wrap-up

  • Workers' console methods now preserve the exception name, message, and stack in logs
  • If you use Workers Observability, dashboard logs are automatically structured with this info
  • Tail Workers receive the error detail in a new errorInfo array

This is great news if you're debugging Worker errors or if you've built log monitoring on top of Tail Workers!