fix(http): respect response Content-Type during JSON parsing#98
Open
GBOYEE wants to merge 1 commit into
Open
Conversation
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
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.
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 tonullwhen parsing fails. This makes it hard to distinguish between:Solution
New error code
INVALID_CONTENT_TYPEtoGuildPassErrorCodeenumContent-Type aware parsing
getContentTypeCategory()helper that inspects theContent-TypeheaderparseSuccessResponse():application/json: parse normally, throwINVALID_RESPONSEif body is malformedINVALID_CONTENT_TYPEif body is not JSONparseErrorResponse()that respects Content-Type for error response parsingGraceful 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 validationdescribe block:INVALID_CONTENT_TYPEfor text/html with non-JSON bodyINVALID_CONTENT_TYPEfor text/plain with non-JSON bodyINVALID_CONTENT_TYPEfor missing content-type with non-JSON bodyINVALID_RESPONSEwhen content-type is JSON but body is malformedFiles Changed
src/errors/errorCodes.ts— addedINVALID_CONTENT_TYPEcodesrc/http/httpClient.ts— content-type aware parsingtests/httpClient.test.ts— 6 new tests