Skip to content

[Workers] Clarify passThroughOnException proxy usage#31304

Draft
irvinebroque wants to merge 3 commits into
productionfrom
fix-passthrough-on-exception-docs
Draft

[Workers] Clarify passThroughOnException proxy usage#31304
irvinebroque wants to merge 3 commits into
productionfrom
fix-passthrough-on-exception-docs

Conversation

@irvinebroque

Copy link
Copy Markdown
Contributor

Summary

Updates Workers documentation for passThroughOnException() to clarify proxy behavior when an origin fetch(request) fails after consuming the request body. Adds safer examples that catch origin fetch failures and return a 5xx response instead of relying on runtime fallback to replay the consumed body.

Documentation checklist

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/workers/ @cloudflare/workers-docs, @GregBrimble, @irvinebroque, @mikenomitch, @korinne, @WalshyDev, @cloudflare/deploy-config, @cloudflare/product-owners, @cloudflare/wrangler, @MattieTK, @cloudflare/dev-plat-leads, @vy-ton
/src/content/docs/workers/observability/ @irvinebroque, @mikenomitch, @nevikashah, @cloudflare/product-owners, @vy-ton

@ask-bonk ask-bonk Bot added the documentation Documentation edits label Jun 8, 2026

@ask-bonk ask-bonk Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found. The changes accurately clarify the proxy behavior, add safer examples, and fix the outdated 'clone the request body' recommendation. Build passes.

@ask-bonk

ask-bonk Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

I've reviewed PR #31304 and posted an approving review.

Summary of Changes

The PR clarifies passThroughOnException() behavior across three Workers docs pages when used in proxy scenarios:

  1. runtime-apis/context.mdx — Updates the caution admonition to explain that origin fetch() failures after the request body is consumed cannot be replayed by the runtime fallback. Removes the outdated "clone the request body" recommendation and adds a safer try…catch example that returns 502 Bad Gateway.
  2. observability/errors.mdx — Generalizes the intro from event.passThroughOnException to passThroughOnException(), adds the same warning about consumed bodies, and updates both Module and Service Worker tab examples to catch origin fetch errors.
  3. best-practices/workers-best-practices.mdx — Adds a one-sentence note warning that proxy migrations should catch origin fetch() failures and return 5xx responses.

Validation

  • pnpm run check passes with 0 errors.
  • No unescaped MDX characters, broken links, or frontmatter issues.
  • Labels updated: added documentation (already had product:workers).

github run

@irvinebroque irvinebroque marked this pull request as ready for review June 9, 2026 01:05
@cloudflare-docs-bot

cloudflare-docs-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Review

✅ No style-guide issues found in commit 502aa57.

Commands

Only codeowners can run commands. Post a comment with the command to trigger it.

Command Description
/review Runs a review now. Incremental if a prior review exists, full if not.
/full-review Re-reviews the entire PR diff from scratch, ignoring incremental history. Useful after a rebase, when you want a fresh review, or if the bot gets out of sync and reports issues that no longer exist.

@irvinebroque irvinebroque marked this pull request as draft June 9, 2026 01:06
The Workers runtime uses streaming for request and response bodies. It does not buffer the body. If an exception occurs after the body has been consumed, `passThroughOnException()` cannot send the body again.

If this causes issues, we recommend cloning the request body and handling exceptions in code. This will protect against uncaught code exceptions. However some exception times such as exceed CPU or memory limits will not be mitigated.
For proxy Workers, avoid relying on the runtime fallback for failures from `fetch(request)`. If the origin fetch throws after consuming the request body, the fallback request may reach your origin without the original body and fail with an unrelated `4xx` error. Catch origin fetch errors and return a `5xx` response instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't call them "proxy Workers" like it is capitalized thing, it should be like "for a Worker that is proxying requests to an origin" or something similar.

});

async function handleRequest(request) {
// An error here will return the origin response, as if the Worker wasn’t present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we still need this comment?

export default {
async fetch(request, env, ctx) {
ctx.passThroughOnException();
// an error here will return the origin response, as if the Worker wasn't present

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we still need this comment?

By using [`event.passThroughOnException`](/workers/runtime-apis/context/#passthroughonexception), a Workers application will forward requests to your origin if an exception is thrown during the Worker's execution. This allows you to add logging, tracking, or other features with Workers, without degrading your application's functionality.
By using [`passThroughOnException()`](/workers/runtime-apis/context/#passthroughonexception), a Workers application can forward requests to your origin if an exception is thrown during the Worker's execution. This allows you to add logging, tracking, or other features with Workers, without degrading your application's functionality.

If your Worker proxies to origin in the normal request path, catch errors from the origin fetch and return a `5xx` response. If `fetch(request)` throws after consuming the request body, `passThroughOnException()` cannot replay the body for the fallback request.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this should be more like — you should still wrap calls to fetch() in try/catch — if the fetch() request fails, it then the request will still fail. ctx.passThroughOnException() only "passes through" for unhandled exceptions that do not come from calling fetch() and making a request to the origin server. It will not reply the request to the origin

Comment thread src/content/docs/workers/best-practices/workers-best-practices.mdx Outdated
@irvinebroque

Copy link
Copy Markdown
Contributor Author

/bonk review my comments and improve the language to be most clear

Co-authored-by: irvinebroque <irvinebroque@users.noreply.github.com>
@ask-bonk

ask-bonk Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Addressed all four review comments with clearer language and cleaner examples.

Changes made:

  1. src/content/docs/workers/runtime-apis/context.mdx:249 — Changed "For proxy Workers" to "For a Worker that proxies requests to an origin" to avoid capitalizing it as a product/category.

  2. src/content/docs/workers/observability/errors.mdx:341 — Rewrote the explanation to distinguish what passThroughOnException() actually handles:

    ctx.passThroughOnException() forwards requests for unhandled exceptions in your Worker code, not for errors from the origin fetch(). When proxying requests to an origin, wrap fetch(request) in try...catch and return a 5xx response on failure. If the origin fetch() throws after consuming the request body, passThroughOnException() cannot replay the body.

  3. src/content/docs/workers/observability/errors.mdx:351 — Removed the // Add logging, tracking, or other logic here. comment from the Module Worker example.

  4. src/content/docs/workers/observability/errors.mdx:377 — Removed the same comment from the Service Worker example.

pnpm run check passes with 0 errors.

github run


`passThroughOnException()` is a fail-open mechanism that sends requests to your origin when your Worker throws an unhandled exception. While it can be useful during migration from an origin server, it hides bugs and makes debugging difficult. Use explicit `try...catch` blocks with structured error responses instead.


Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

async fetch(request, env, ctx) {
// Proxy to origin on unhandled/uncaught exceptions
ctx.passThroughOnException();
throw new Error("Oops");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/bonk this example should still include try/catch and a fetch() request

actually you can remove it and just use the one below - and you don't need the sentence on line 266

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

Labels

documentation Documentation edits product:workers Related to Workers product size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants