Skip to content

fix(ext/runtime): return 500 instead of 502 for invalid handler response - #720

Open
davidbirdsong wants to merge 1 commit into
mainfrom
fix/ext-runtime-invalid-response-status
Open

fix(ext/runtime): return 500 instead of 502 for invalid handler response#720
davidbirdsong wants to merge 1 commit into
mainfrom
fix/ext-runtime-invalid-response-status

Conversation

@davidbirdsong

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

When a serve() handler (main worker, event worker, or a user function) resolves with something that isn't a Response object, ext/runtime/js/http.js falls back to a bare 502 Bad Gateway with a generic "Bad Gateway" body.

502 implies edge-runtime received an invalid response from an upstream/gateway target. That's not what's happening here: the "handler" runs in-process, and the violation is edge-runtime's own contract (a handler must resolve to a Response) not being honored. Framing it as a gateway error also makes this failure indistinguishable from unrelated, real gateway-level 502s, which complicates 502 triage.

What is the new behavior?

  • The fallback now returns 500 Internal Server Error instead of 502 Bad Gateway, which more accurately reflects that the fault is internal, not upstream.
  • The response carries a new header, x-edge-runtime-error: invalid-handler-response, so this specific failure mode can be distinguished from other 500s and from unrelated 502s in logs/metrics without relying on status code alone.
  • badGatewayError() is renamed to invalidHandlerResponseError() to match what it actually represents.

Additional context

No functional change to the "handler threw an exception" path (internalServerError(), still 500, unchanged) — this only affects the "handler resolved to a non-Response value" path.

@davidbirdsong

Copy link
Copy Markdown
Author

This change is part of the following stack:

Change managed by git-spice.

A handler resolving to a non-Response value is a contract violation on our side, not a bad response from an upstream gateway target. Tag it with x-edge-runtime-error so it's distinguishable from other 500s and from the unrelated upsteram 502s.
@davidbirdsong
davidbirdsong force-pushed the fix/ext-runtime-invalid-response-status branch from aace5d1 to 4d77e3d Compare August 21, 2026 21:31
Comment thread ext/runtime/js/http.js
function invalidHandlerResponseError() {
return new Response("Internal Server Error", {
status: 500,
headers: { "x-edge-runtime-error": "invalid-handler-response" },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're introducing a new header,
I believe we should use the sb-error-code or discuss to find an better edge-runtime error pattern

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants