Skip to content

#313 apiRequest propagates a raw SyntaxError for a malformed 2xx JSON…#400

Open
felladaniel36-hash wants to merge 1 commit into
AnchorNet-Org:mainfrom
felladaniel36-hash:#313-apiRequest-propagates-a-raw-SyntaxError-for-a-malformed-2xx-JSON-body-instead-of-a-descriptive-ApiRequestError-FIX
Open

#313 apiRequest propagates a raw SyntaxError for a malformed 2xx JSON…#400
felladaniel36-hash wants to merge 1 commit into
AnchorNet-Org:mainfrom
felladaniel36-hash:#313-apiRequest-propagates-a-raw-SyntaxError-for-a-malformed-2xx-JSON-body-instead-of-a-descriptive-ApiRequestError-FIX

Conversation

@felladaniel36-hash

Copy link
Copy Markdown

Description

Fixed apiRequest so malformed JSON in a successful 2xx API response no longer leaks a raw SyntaxError such as Unexpected end of JSON input.

Previously, apiRequest directly returned await res.json() for successful responses. If the server returned an empty body, HTML, or truncated JSON with a 2xx status, callers received a raw parser error instead of the app’s friendly API error format.

This PR adds defensive parsing for successful JSON responses and throws a descriptive ApiRequestError with:

  • status: the response status, e.g. 200
  • code: INVALID_RESPONSE
  • message: The server returned an invalid response.

The existing non-2xx parseError behavior is unchanged, and apiTextRequest remains unaffected.

A regression test was added to verify that a 200 response whose json() rejects does not surface a raw SyntaxError.

Closes: malformed 2xx JSON body issue.

Checklist

  • I added a CHANGELOG.md entry under the next ## [x.y.z] section

    (see the **Format** note at the top of `CHANGELOG.md`), **or** this PR
    
    is docs-only / test-only / internal tooling and doesn't change
    
    user-facing behavior.
    
  • Tests added/updated for the change.

CLOSE #313

…rmed 2xx JSON body instead of a descriptive ApiRequestError FIXED
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.

apiRequest propagates a raw SyntaxError for a malformed 2xx JSON body instead of a descriptive ApiRequestError

1 participant