Repository navigation
fix: handle primitive JSON error bodies without throwing - #820
Merged
wolfy1339 merged 1 commit intoJun 29, 2026
Merged
Conversation
When an HTTP error response has `content-type: application/json` but the JSON body is a primitive (`null`, a number, a boolean), `toErrorMessage()` threw a raw `TypeError` from `"message" in data` before `RequestError` was constructed, so callers lost the `status`, `request`, and `response` fields. Guard the object-shaped branch with `typeof data === "object" && data !== null` so primitives fall through to the generic `Unknown error: ...` message and a proper `RequestError` is thrown. Also widen the parameter type to `unknown`, which reflects that the parsed body can be any JSON value. Fixes #819
|
👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with |
wolfy1339
approved these changes
Jun 29, 2026
|
🎉 This PR is included in version 10.0.11 🎉 The release is available on: Your semantic-release bot 📦🚀 |
1 task
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.
Fixes #819
What was wrong
When an HTTP error response has
content-type: application/jsonbut the JSON body is a primitive value such asnull,42, orfalse,toErrorMessage()threw a rawTypeErrorwhile building the error message:This happens at
"message" in data— theinoperator is only valid on objects. Because it throws beforeRequestErroris constructed, callers lose the documentedstatus,request, andresponsefields.The declared parameter type (
Record<string, unknown>) hid the problem at compile time, butgetResponseData()returnsany, so a primitive JSON body reachestoErrorMessage()at runtime.Fix
Guard the object-shaped branch with
typeof data === "object" && data !== nullbefore usingin. Primitive JSON bodies now fall through to the existing genericUnknown error: <value>path and a properRequestError(HttpError) is thrown withstatus/request/responseintact. The parameter type is widened tounknownto reflect that a parsed JSON body can be any value.This does not change behavior for the common object-shaped GitHub error responses; it only affects primitive bodies that previously crashed (custom
fetch, custombaseUrl, GHES/proxy responses, etc.).Test
Adds a regression test covering
null,42, andfalseerror bodies, asserting each rejects with anHttpErrorcarryingstatus: 400, the expectedUnknown error: <value>message, and a definedresponse. Verified it fails onmain(rawTypeError) and passes with this change; the full suite remains green.