Skip to content

Respect response content type during JSON parsing #86

Description

@Lakes41

Difficulty: Hard

Type: Bug

Summary

Improve HTTP response parsing so non-JSON responses produce clearer errors instead of generic JSON parsing failures.

Current Behaviour

Successful responses are parsed with response.json() unless the status is 204, 205, or the content length is 0. Error responses also attempt JSON parsing and silently fall back to null when parsing fails.

Expected Behaviour

The SDK should inspect response Content-Type and produce clearer errors when an endpoint returns HTML, plain text, or malformed JSON unexpectedly.

Suggested Implementation

Add a response parsing helper that checks Content-Type. For expected JSON endpoints, parse JSON when content type includes application/json; otherwise throw a GuildPassError with INVALID_RESPONSE and safe metadata. Preserve current support for empty successful responses.

Files or Areas Likely Affected

  • src/http/httpClient.ts
  • src/errors/errorCodes.ts
  • tests/httpClient.test.ts
  • docs/sdk-guide.md

Acceptance Criteria

  • Successful non-empty JSON responses still parse correctly
  • Empty 204 and 205 responses remain supported
  • HTML or plain-text success responses produce a clear invalid response error
  • Malformed JSON produces a clear invalid response error
  • Error responses with non-JSON bodies still produce safe GuildPassError values
  • Tests cover JSON, empty, non-JSON, and malformed responses

Additional Notes

Do not include full raw response bodies in errors by default, as backend responses may contain sensitive details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official Campaign

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions