Repository navigation
fix(core): reject non-JSON-RPC response bodies with ApiError - #1171
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 4b694bb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
belaboe
approved these changes
Nov 2, 2026
belaboe
left a comment
Contributor
There was a problem hiding this comment.
Thanks! Checking for the envelope rather than response.ok is the right call. Some non-blocking thoughts:
- Could the fallback also read a string
error? Our own CORS 403 and dev-server 502 bodies use{"error": ...}and now show up as a bareHTTP 403/HTTP 502. - The gateway's
messagebecomes a wire-safeApiError.message. If server code rethrows it, raw gateway text (for example an IAM 403 with an ARN) reaches the end user. Could we clip it, or keep it out ofmessage, aserrors.tsasks? - Should 429/503/504 set
retriable: true? - Nit: for a 2xx with no envelope, could the message name the real status (
HTTP 200)? And should an empty 204 resolveundefined?
Follow-up: the Dart client has the same "resolves null on a gateway body" bug, so it's worth an issue.
harsh62
added a commit
that referenced
this pull request
Nov 6, 2026
Brings in 14 commits from main, among them "fix(core): return Invalid Request for a null JSON-RPC body" (#1143), "fix(core): reject non-JSON-RPC response bodies with ApiError" (#1171), "feat(bb-logger,core): backend Lambda log lines carry the invocation's requestId" (#1325), #1131, #1132, #1142, #1167, #1173, #1178, #1214, #494 and CI changes (#946, #970, #971). packages/core/src/client/index.test.ts and lambda-handler.test.ts: keep both sides' tests (this branch's httpErrorStatus, namespace identity and per-call fetch suites; main's non-JSON-RPC body and request id suites). packages/core/src/api.ts: the httpErrorStatus docs now say what the client does since #1171 when a Hosting error page replaces a 403 / 404: it rejects with a bare ApiError carrying the HTTP status, instead of response.json() throwing. # Conflicts: # packages/core/src/client/index.test.ts # packages/core/src/lambda-handler.test.ts
2 tasks done
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
When API Gateway (or another proxy) answers a request itself, the body has no JSON-RPC envelope:
502 {"message":"Internal server error"},429 {"message":"Too Many Requests"},403 {"message":"Forbidden"},504 {"message":"Endpoint request timed out"}. The typed client inpackages/core/src/client/index.tspasses that body todecodeRpcResponse(packages/core/src/rpc.ts), which only checkserrorand returnsrpc.result, so the call resolvesundefinedand the caller treats a throttled, forbidden or timed-out request as success. A non-JSON body (an HTML error page) instead rejects with a rawSyntaxErrorfromresponse.json()with no status.Expected: the call rejects with an
ApiErrorcarrying the HTTP status.Issue #, if available:
Changes
packages/core/src/rpc.ts:decodeRpcResponse(body, httpStatus?)throws anApiErrorwhen the body is not a JSON-RPC 2.0 envelope (jsonrpc !== '2.0'). The status ishttpStatuswhen it is not 2xx, else502; the message is the body's stringmessagewhen present, elseHTTP <status>. The check keys on the envelope, not onresponse.ok(methods may set a custom status and still return a result) or on a missingresult(void methods omit it). JSDoc extended.packages/core/src/client/index.ts: read the body as text and parse it, treating unparseable text as no envelope, then passresponse.statustodecodeRpcResponse.packages/core/src/client/index.test.ts: stubbed-fetch tests for gateway502/429/403/504bodies, an HTML body, and a200body without an envelope; controls for a void result and for result envelopes sent with202and418.packages/core/README.md: document how the client reports a non-envelope response..changeset/core-client-non-rpc-error-bodies.md: patch for@aws-blocks/core.Validation
packages/core/src/client/index.test.ts: fail before the change (6 of 8 fail: gateway bodies resolveundefined— "Missing expected rejection"; the HTML body rejects withSyntaxError: Unexpected token '<'instead ofApiError; the two controls pass) and pass after.npm run build,@aws-blocks/corenpm test,npm run lint,npm run lint:deps: pass.npm test(all workspaces) andnpm run test:e2e:localfrom the repo root: pass.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.