Repository navigation
fix(wanderweird): keep the gateway up when an upstream is not JSON - #488
Draft
cursor[bot] wants to merge 1 commit into
Draft
cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Express 4 does not catch rejected async handlers. Reading the body with json().catch(text()) throws after a failed parse, and /graphql never used the 502 helper. Read the body once and send GraphQL through the same path. Co-authored-by: Sebastian Gedda <sebastian.gedda@gmail.com>
This branch has not been deployed
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.
Problem
The gateway change that answers 502 when an upstream is down still crashes the process.
Express 4 does not catch a rejected async handler.
proxyJsononly caughtfetchthrowing (ECONNREFUSED). The body was then read withresponse.json().catch(() => response.text()). After a failed JSON parse the body is already consumed, sotext()throwsBody is unusable. That rejection is outside the try/catch, so Node exits and every other gateway route dies with it./graphqlnever usedproxyJson. A down GraphQL upstream (ECONNREFUSEDon port 4206) rejects the handler the same way.Trigger: start the gateway, leave GraphQL down, and
POST /graphql. Or call any proxied route whose upstream returns HTML or plain text (a 502 error page).Solution
Read the upstream body once as text, then
JSON.parse. Connection failures and unreadable bodies return 502 JSON./graphqluses that helper.Changes
proxyJson/readUpstreamBodyand route GraphQL through them.How it works
Non-JSON and unreachable upstreams resolve instead of rejecting:
GraphQL uses the same helper as the REST routes:
A down GraphQL service now answers 502 JSON:
Testing
npx jest tests/gateway-proxy-json.test.ts— 5 passed (JSON body, HTML body, empty body,ECONNREFUSED→ 502, non-JSON status preserved).npm test— 138 suites passed, 1 skipped; 1196 passed, 7 skipped.