Skip to content

fix: return 502/504 instead of 204 on navigation failure#1598

Open
kitsuyui wants to merge 1 commit into
mainfrom
fix/navigation-error-status
Open

fix: return 502/504 instead of 204 on navigation failure#1598
kitsuyui wants to merge 1 commit into
mainfrom
fix/navigation-error-status

Conversation

@kitsuyui

@kitsuyui kitsuyui commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Summary

When page.goto() throws a network error (DNS failure, connection refused, TLS error, etc.), the proxy was silently returning HTTP 204 No Content. This is misleading: 204 means the server explicitly sent "no content", but a thrown error means the upstream was unreachable.

This PR changes the behaviour:

  • TimeoutError from Playwright → 504 Gateway Timeout
  • Other network errors (DNS, connection refused, TLS) → 502 Bad Gateway

Distinguishing errors from valid 204 responses

page.goto() also throws when the server legitimately returns 204 No Content, because Playwright aborts the document load when there is no HTML body to parse. A naive error→502 mapping would break the existing 204 pass-through.

To solve this, a response event listener captures the main-frame HTTP response before page.goto() throws. If a response was captured, the server did reply — Playwright simply could not load a document from it — so the captured response is forwarded as-is. Only when no response was captured at all (genuine connection failure) does the proxy return 502/504.

Changes

  • src/render/index.ts:

    • captureNavigationResponse() — attaches a response event listener and exposes the captured main-frame response.
    • classifyNavigationError() — maps a caught error to 502 or 504.
    • tryGoto() — wraps page.goto() in a try/catch using the helpers above.
    • navigatePage() — orchestrates the capture and classifies the outcome.
    • hasNoBody() / readRenderedBody() — skip response.body() for status codes that carry no body by spec (204, 304, 1xx).
    • errorRenderResult() — builds an empty error response for 502/504.
  • src/render/index.spec.ts: Add a test that navigates to http://127.0.0.1:19999/ (nothing listening) and expects HTTP 502.

Verification

All 36 tests pass, including the pre-existing "can handle empty response" test (204 pass-through) and the new 502 test.

Trade-offs

The response-event approach is more robust than inspecting error message strings, but it does add a response listener per request. The listener is always detached before the function returns, so there is no leak across requests.

@kitsuyui kitsuyui force-pushed the fix/navigation-error-status branch from 4b901d0 to ebcc6a2 Compare June 4, 2026 15:56
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

gh-counter

PR gate

Removed Added +/-
TODO/expect-error 0 0 0

Repo dashboard

main (b5aa171) #1598 (0bb7cdc) +/-
TODO/expect-error 4 4 0

Reported by gh-counter

When page.goto() throws a network error (DNS failure, connection
refused, TLS error), the proxy now returns an appropriate error
status rather than 204 No Content:
- TimeoutError → 504 Gateway Timeout
- Other network errors → 502 Bad Gateway

A response event listener captures the main-frame server reply before
page.goto() completes. If the server did reply but Playwright aborted
the document load (e.g. because the origin returned 204 No Content),
the captured response is forwarded instead of treating it as an error.

Add a test that verifies a connection-refused URL returns 502.
@kitsuyui kitsuyui force-pushed the fix/navigation-error-status branch from ebcc6a2 to d02ea0f Compare June 5, 2026 17:29
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

Code Metrics Report

main (b5aa171) #1598 (0bb7cdc) +/-
Coverage 87.5% 87.9% +0.3%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 6s 7s +1s
Details
  |                     | main (b5aa171) | #1598 (0bb7cdc) |  +/-  |
  |---------------------|----------------|-----------------|-------|
+ | Coverage            |          87.5% |           87.9% | +0.3% |
  |   Files             |             11 |              11 |     0 |
  |   Lines             |            177 |             191 |   +14 |
+ |   Covered           |            155 |             168 |   +13 |
- | Code to Test Ratio  |          1:0.5 |           1:0.5 |  -0.1 |
  |   Code              |           1428 |            1491 |   +63 |
+ |   Test              |            819 |             828 |    +9 |
- | Test Execution Time |             6s |              7s |   +1s |

Code coverage of files in pull request scope (100.0% → 97.5%)

Files Coverage +/- Status
src/render/index.ts 97.5% -2.5% modified

Reported by octocov

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.

1 participant