Skip to content

fix(core): reject non-JSON-RPC response bodies with ApiError - #1171

Merged
xyos merged 1 commit into
aws:mainfrom
xyos:fix/core-client-non-rpc-error-bodies
Nov 6, 2026
Merged

xyos merged 1 commit into
aws:mainfrom
xyos:fix/core-client-non-rpc-error-bodies

Conversation

@xyos

@xyos xyos commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

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 in packages/core/src/client/index.ts passes that body to decodeRpcResponse (packages/core/src/rpc.ts), which only checks error and returns rpc.result, so the call resolves undefined and the caller treats a throttled, forbidden or timed-out request as success. A non-JSON body (an HTML error page) instead rejects with a raw SyntaxError from response.json() with no status.

Expected: the call rejects with an ApiError carrying the HTTP status.

Issue #, if available:

Changes

  • packages/core/src/rpc.ts: decodeRpcResponse(body, httpStatus?) throws an ApiError when the body is not a JSON-RPC 2.0 envelope (jsonrpc !== '2.0'). The status is httpStatus when it is not 2xx, else 502; the message is the body's string message when present, else HTTP <status>. The check keys on the envelope, not on response.ok (methods may set a custom status and still return a result) or on a missing result (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 pass response.status to decodeRpcResponse.
  • packages/core/src/client/index.test.ts: stubbed-fetch tests for gateway 502/429/403/504 bodies, an HTML body, and a 200 body without an envelope; controls for a void result and for result envelopes sent with 202 and 418.
  • 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

  • New tests in packages/core/src/client/index.test.ts: fail before the change (6 of 8 fail: gateway bodies resolve undefined — "Missing expected rejection"; the HTML body rejects with SyntaxError: Unexpected token '<' instead of ApiError; the two controls pass) and pass after.
  • npm run build, @aws-blocks/core npm test, npm run lint, npm run lint:deps: pass.
  • npm test (all workspaces) and npm run test:e2e:local from the repo root: pass.

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@xyos
xyos requested a review from a team as a code owner October 30, 2026 00:36
@changeset-bot

changeset-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4b694bb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-blocks/core Patch

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 belaboe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Checking for the envelope rather than response.ok is the right call. Some non-blocking thoughts:

  1. 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 bare HTTP 403 / HTTP 502.
  2. The gateway's message becomes a wire-safe ApiError.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 of message, as errors.ts asks?
  3. Should 429/503/504 set retriable: true?
  4. Nit: for a 2xx with no envelope, could the message name the real status (HTTP 200)? And should an empty 204 resolve undefined?

Follow-up: the Dart client has the same "resolves null on a gateway body" bug, so it's worth an issue.

@xyos
xyos merged commit 0cf1aa2 into aws:main Nov 6, 2026
42 checks passed
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
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