Skip to content

fix(http): respect response Content-Type during JSON parsing#98

Open
GBOYEE wants to merge 1 commit into
Adamantine-guild:mainfrom
GBOYEE:fix/content-type-validation-86
Open

fix(http): respect response Content-Type during JSON parsing#98
GBOYEE wants to merge 1 commit into
Adamantine-guild:mainfrom
GBOYEE:fix/content-type-validation-86

Conversation

@GBOYEE

@GBOYEE GBOYEE commented Jun 25, 2026

Copy link
Copy Markdown

Summary

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

Problem

Currently, successful responses are parsed with response.json() regardless of Content-Type, and error responses silently fall back to null when parsing fails. This makes it hard to distinguish between:

  1. A server returning HTML instead of JSON (e.g., error page)
  2. A server returning malformed JSON
  3. A server returning an empty response

Solution

New error code

  • Added INVALID_CONTENT_TYPE to GuildPassErrorCode enum

Content-Type aware parsing

  • Added getContentTypeCategory() helper that inspects the Content-Type header
  • Updated parseSuccessResponse():
    • application/json: parse normally, throw INVALID_RESPONSE if body is malformed
    • Other types (html, text, missing): attempt JSON parse as graceful fallback, throw INVALID_CONTENT_TYPE if body is not JSON
  • Added parseErrorResponse() that respects Content-Type for error response parsing

Graceful fallback

If the Content-Type is wrong but the body IS valid JSON, the SDK still returns the data — since some servers misconfigure their Content-Type headers.

Tests Added

6 new tests in HttpClient Content-Type validation describe block:

  • ✅ Throws INVALID_CONTENT_TYPE for text/html with non-JSON body
  • ✅ Throws INVALID_CONTENT_TYPE for text/plain with non-JSON body
  • ✅ Parses JSON body even with wrong content-type (graceful fallback)
  • ✅ Parses normally when content type is application/json
  • ✅ Throws INVALID_CONTENT_TYPE for missing content-type with non-JSON body
  • ✅ Throws INVALID_RESPONSE when content-type is JSON but body is malformed

Files Changed

  • src/errors/errorCodes.ts — added INVALID_CONTENT_TYPE code
  • src/http/httpClient.ts — content-type aware parsing
  • tests/httpClient.test.ts — 6 new tests

When a server returns a non-JSON Content-Type (text/html, text/plain,
or missing), the SDK now throws GuildPassError with INVALID_CONTENT_TYPE
instead of a generic SyntaxError. This makes it clear that the issue is
a content-type mismatch, not malformed JSON.

Changes:
- Add INVALID_CONTENT_TYPE error code to GuildPassErrorCode enum
- Add getContentTypeCategory() helper to inspect Content-Type headers
- Update parseSuccessResponse() to check Content-Type before parsing
  - application/json: parse normally, throw INVALID_RESPONSE on failure
  - other types: attempt JSON parse as fallback, throw INVALID_CONTENT_TYPE
    if body is not JSON
- Add parseErrorResponse() that respects Content-Type for error bodies
- Add 6 tests covering all Content-Type validation scenarios

Closes Adamantine-guild#86
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.

Respect response content type during JSON parsing

1 participant