fix: return 502/504 instead of 204 on navigation failure#1598
Open
kitsuyui wants to merge 1 commit into
Open
Conversation
4b901d0 to
ebcc6a2
Compare
gh-counterPR gate
Repo dashboard
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.
ebcc6a2 to
d02ea0f
Compare
Code Metrics Report
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%)
Reported by octocov |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
TimeoutErrorfrom Playwright → 504 Gateway TimeoutDistinguishing 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
responseevent listener captures the main-frame HTTP response beforepage.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 aresponseevent listener and exposes the captured main-frame response.classifyNavigationError()— maps a caught error to 502 or 504.tryGoto()— wrapspage.goto()in a try/catch using the helpers above.navigatePage()— orchestrates the capture and classifies the outcome.hasNoBody()/readRenderedBody()— skipresponse.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 tohttp://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
responselistener per request. The listener is always detached before the function returns, so there is no leak across requests.