Accept pre-parsed JSON body in HttpError.from()#1
Closed
freshlogic wants to merge 2 commits into
Closed
Conversation
GraphQL servers always return 200 OK with errors carried in data.errors[],
and some REST APIs (FedEx, etc.) do the same — a 2xx response carries an
application-level errors envelope. Callers that need to throw on those
already have the parsed body in hand from response.json() and just want to
attach it to an HttpError.
HttpError.fromJson(response, json) is a synchronous factory that sets
err.json = json and err.text = JSON.stringify(json), leaving the message
at the default "${status} ${statusText}" so callers can override it with
whatever aggregated form makes sense for their API.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Coverage Report for CI Build 25706064232Coverage remained the same at 100.0%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
Closing — went with the clone-before-consume pattern on the caller side instead, no http-error change needed. |
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
`HttpError.from(response)` now accepts an optional second argument: an already-parsed JSON body. When supplied, the factory skips the body read and sets `err.json` and `err.text` from the supplied value.
This covers the common case of an application-level error envelope on an otherwise-ok response — GraphQL servers (always 200 OK with `data.errors[]`) and some REST APIs (FedEx Rate, etc.) that return 200 with an `errors[]` envelope. The caller has already called `response.json()` to inspect the body and now wants to throw with the body attached.
Before:
```js
const err = new HttpError(res);
err.json = json;
err.text = JSON.stringify(json);
err.message = json.errors.map(e => e.message).join('; ');
throw err;
```
After:
```js
const err = await HttpError.from(res, json);
err.message = json.errors.map(e => e.message).join('; ');
throw err;
```
Design notes
Test plan
🤖 Generated with Claude Code