In Cloudflare Workers, when does ctx.passThroughOnException() send a request on to the origin, and why can a proxied POST then reach the origin without its body?
answer
- fail open, not fail closed
- only the Worker's own exceptions
- bodies are streamed, not buffered
- catch the origin fetch yourself
basics
~20 sAfter ctx.passThroughOnException(), an unhandled exception in the Worker's code sends the request to the origin as if the Worker were absent. Bodies are streamed, so if an origin fetch() consumed the body and then threw, the fallback arrives without it.
solid answer
~40 s`ctx.passThroughOnException()` makes a Worker fail open: if its own code throws an unhandled exception, the runtime forwards the request to the origin as though the Worker had never run. It only helps when a route places the Worker in front of an origin, and it does not cover CPU or memory limit failures. Because the runtime streams bodies rather than buffering them, a `POST` body already consumed by `fetch(request)` cannot be replayed: if that origin call throws, the fallback reaches the origin without its body and fails with an unrelated 4xx. So wrap the origin `fetch()` in `try...catch` and return a `502`. Failing open also skips whatever validation or auth the Worker enforces, so a guarding Worker should not use it.
code
javascript · 12 linesexport default {
async fetch(request, env, ctx) {
ctx.passThroughOnException();
try {
return await fetch(request);
} catch (err) {
console.error("origin fetch failed", err);
return new Response("Bad Gateway", { status: 502 });
}
},
};go deeper
Recall that ctx.passThroughOnException() makes the Worker fail open to the origin when its own code throws.
Explain the mechanics: streamed bodies cannot be replayed, origin fetch errors need their own try...catch, and resource-limit failures are not covered.
Show production judgment: use it during a migration, keep explicit error handling, and recognise when a fallback request without its body is misleading your debugging.
Treat fail-open as a policy choice: a team should allow it only on Workers whose checks the origin would repeat, never on the security boundary.
## Failing open `ctx.passThroughOnException()` changes what happens when a Worker's own code throws an **unhandled exception**. Normally the client gets an error response from the Worker. After the call, the runtime **fails open** instead: it sends the request on to the origin server as though the Worker had never been invoked. The method exists for Workers placed in front of an existing service, where it is better for the origin to answer than for the request to fail. Take a leaderboard Worker deployed on a route in front of an older leaderboard origin. It adds caching headers and validates `POST /scores` bodies before passing them on. If a bug in the validator throws, `passThroughOnException()` lets the origin take the request rather than showing the player an error. ## What it covers and what it does not | Situation | Effect of `passThroughOnException()` | |---|---| | The Worker's own code throws and nothing catches it | The request goes to the origin as if the Worker were absent | | The origin `fetch()` throws after reading the request body | The fallback request reaches the origin **without its body**, often failing with an unrelated 4xx | | The Worker exceeds its CPU or memory limit | Not covered; the request fails as usual | | The Worker is the origin, on a Custom Domain or workers.dev | There is no origin behind it to fall back to | ## Why the body goes missing The Workers runtime **streams** request and response bodies; it does not buffer them. A `POST /scores` body that the Worker has already read, or that a `fetch(request)` to the origin has already started sending, cannot be read a second time. If that origin call then throws and the runtime falls back, the fallback request carries no body, and the origin rejects it with an error that has nothing to do with the real fault. Debugging then starts in the wrong place. The documented pattern is to treat origin failures explicitly: ```javascript export default { async fetch(request, env, ctx) { ctx.passThroughOnException(); try { return await fetch(request); } catch (err) { console.error("origin fetch failed", err); return new Response("Bad Gateway", { status: 502 }); } }, }; ``` The `try...catch` turns an origin failure into a clear `502`, while `passThroughOnException()` still covers bugs elsewhere in the Worker. ## The defender's view Failing open is a security decision as much as a reliability one: - **Checks are skipped.** Anything the Worker enforces, such as input validation, authentication or rate checks on `POST /scores`, is bypassed when an exception sends the request straight to the origin. A Worker that guards access should fail closed and must not call `passThroughOnException()`. - **Bugs are hidden.** Requests keep succeeding through the origin, so an exception that fires on every request can go unnoticed. Cloudflare's own best practices advise against using it as error handling and recommend explicit `try...catch` with structured error responses. - **The origin must stay reachable.** Falling back only works when the origin still accepts direct traffic from Cloudflare for that route, which is worth checking before relying on it. ## When to use it 1. **During a migration**, while a new Worker gradually takes over logic from an existing origin and an unexpected exception should not become an outage. 2. **For additive features** such as logging or header rewriting, where the origin's answer is always acceptable. 3. **Never as the main error handling**, and never on a Worker that is the security boundary. During a migration, keep the origin's own validation of `POST /scores` in place for as long as the Worker may fail open, so a fallback request is still checked somewhere. Once the Worker's error handling covers its real failure modes with explicit responses, remove the `passThroughOnException()` call; leaving it in turns every future bug into silent traffic to the origin. Reviewers should treat the call as a temporary migration aid with an owner and a removal plan, not as a permanent safety net. In the deprecated Service Worker syntax the same method was `event.passThroughOnException()`; in the module format it sits on `ctx`. A strong senior answer names what triggers the fallback, the lost body, what it does not cover, and why a Worker that enforces rules should not fail open.
- Does ctx.passThroughOnException() help when a Cloudflare Worker exceeds its CPU limit?No. It covers uncaught exceptions thrown by the Worker's code, not failures such as exceeding CPU or memory limits. Those still fail the request, so a Worker near its limits needs its work reduced, not a fail-open switch.
- Why should a Cloudflare Worker that checks authentication avoid passThroughOnException()?Falling back sends the request to the origin as if the Worker were absent, so any check the Worker performs is skipped whenever its code throws. A security boundary should fail closed: catch errors and return an error response instead.
- What does passThroughOnException() do for a Worker served on a Custom Domain?Nothing useful: on a Custom Domain, as on workers.dev, the Worker is the origin, so there is no server behind it to receive the fallback. The method only makes sense on a route in front of an existing origin.
It is like a security gate that swings open when its control box fails: traffic keeps flowing, but every check the gate performed is skipped until someone notices.
- passThroughOnException() replays the original request body to the origin after a failure.
- passThroughOnException() also protects a Worker that exceeds its CPU or memory limit.
- passThroughOnException() is a sound default for every Worker's error handling.
- A Worker that validates auth can safely fail open, since the origin is trusted.
- passThroughOnException() falls back usefully even on a Custom Domain with no origin.