Skip to content

fix: handle primitive JSON error bodies without throwing - #820

Merged
wolfy1339 merged 1 commit into
octokit:mainfrom
CedricConday:fix/primitive-json-error-body
Jun 29, 2026
Merged

wolfy1339 merged 1 commit into
octokit:mainfrom
CedricConday:fix/primitive-json-error-body

Conversation

@CedricConday

Copy link
Copy Markdown
Contributor

Fixes #819

What was wrong

When an HTTP error response has content-type: application/json but the JSON body is a primitive value such as null, 42, or false, toErrorMessage() threw a raw TypeError while building the error message:

TypeError: Cannot use 'in' operator to search for 'message' in null

This happens at "message" in data — the in operator is only valid on objects. Because it throws before RequestError is constructed, callers lose the documented status, request, and response fields.

The declared parameter type (Record<string, unknown>) hid the problem at compile time, but getResponseData() returns any, so a primitive JSON body reaches toErrorMessage() at runtime.

Fix

Guard the object-shaped branch with typeof data === "object" && data !== null before using in. Primitive JSON bodies now fall through to the existing generic Unknown error: <value> path and a proper RequestError (HttpError) is thrown with status/request/response intact. The parameter type is widened to unknown to 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, custom baseUrl, GHES/proxy responses, etc.).

Test

Adds a regression test covering null, 42, and false error bodies, asserting each rejects with an HttpError carrying status: 400, the expected Unknown error: <value> message, and a defined response. Verified it fails on main (raw TypeError) and passes with this change; the full suite remains green.

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
@octokit

octokit Bot commented Jun 29, 2026

Copy link
Copy Markdown

👋 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 Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@wolfy1339 wolfy1339 added the Type: Bug Something isn't working as documented label Jun 29, 2026
@wolfy1339
wolfy1339 merged commit 554e102 into octokit:main Jun 29, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from 🆕 Triage to ✅ Done in 🧰 Octokit Active Jun 29, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 10.0.11 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Type: Bug Something isn't working as documented

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

Primitive JSON error bodies can throw raw TypeError before RequestError is constructed

2 participants