Skip to content

Accept an already-parsed body in HttpError.from() (1.2.0)#3

Merged
freshlogic merged 1 commit into
mainfrom
from-response-json
May 27, 2026
Merged

Accept an already-parsed body in HttpError.from() (1.2.0)#3
freshlogic merged 1 commit into
mainfrom
from-response-json

Conversation

@freshlogic
Copy link
Copy Markdown
Member

Summary

HttpError.from() gains an optional second argument — an already-parsed JSON body:

  • from(response) — unchanged; reads the body via response.clone().
  • from(response, json) — uses the body you already read instead of re-reading the response.

This fixes the common pattern where a 200 response carries an errors[] envelope: once you've read the body with await response.json(), the response stream is consumed, so a second from(response) can't read it (you get a bare "200 OK"). Passing the parsed body — from(response, json) — captures the detail, and the error still carries the response status and cause.

if (!response.ok) {
    throw await HttpError.from(response);
}
const json = await response.json();
if (json.errors?.length) {
    throw await HttpError.from(response, json);
}

Backward compatible — from(response) behavior is identical.

Also

  • Reformatted CHANGELOG.md to Keep a Changelog, matching the other @stores.com packages.

Testing

node --test — 15 pass / 0 fail; eslint clean. New tests cover from(response, json) (uses the passed body without re-reading, JSON:API detail, status-message fallback).

🤖 Generated with Claude Code

`from(response, json)` uses a body you've already read instead of re-reading
the response — for a 200 response with an `errors[]` envelope, where the body
is consumed by `response.json()`. `from(response)` is unchanged. The error
still carries the response status and `cause`.

Also reformats the changelog to Keep a Changelog, matching the other packages.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@freshlogic freshlogic merged commit 33c7501 into main May 27, 2026
2 checks passed
@freshlogic freshlogic deleted the from-response-json branch May 27, 2026 01:46
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 26485767779

Coverage decreased (-3.7%) to 96.341%

Details

  • Coverage decreased (-3.7%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (26 of 28 lines covered, 92.86%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
index.js 28 26 92.86%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 66
Covered Lines: 64
Line Coverage: 96.97%
Relevant Branches: 16
Covered Branches: 15
Branch Coverage: 93.75%
Branches in Coverage %: Yes
Coverage Strength: 5.05 hits per line

💛 - Coveralls

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.

2 participants