Repository navigation
Shorten error bodies which aren't JSON to an HTML page's title, or 512 bytes - #77
Merged
Merged
Conversation
…2 bytes An error response whose body isn't JSON had its whole body in its error, such as a CDN's HTML error page of several kilobytes, which then filled logs. Its detail is now the page's title, for an HTML page, or only its status when it has none, and other text without its surrounding space, cut at 512 bytes, at a character's start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Content-type detection can retain HTML markup or discard useful plain-text error details for valid headers.
1 open issue
What changed in this PR
This PR reduces noisy HTTP client errors by summarizing response bodies that cannot be used as structured JSON errors.
Changes:
- Extract HTML page titles, decode entities, and normalize whitespace.
- Trim other text and truncate it at a UTF-8 boundary after at most 512 bytes, appending an ellipsis.
- Add tests for HTML responses and long multibyte text.
| File | Description |
|---|---|
| do_test.go | Adds coverage for HTML titles, titleless pages, and long UTF-8 bodies. |
| client.go | Centralizes fallback error formatting and shortens response details. |
Review effort: Balanced
- An error body is HTML when the media type of its content type, parsed without its parameters, and in any case, is text/html or application/xhtml+xml, or it starts as an HTML page, so Text/HTML is HTML, and text/plain; name=error.html is plain text Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

When an error response's body isn't JSON, its whole body went into the error. For an HTML error page from a proxy or CDN, that's several kilobytes of markup in every log line and error message. For example, Cloudflare's 504 page in front of
api.openai.comfilled a log line in omodie.Changes
A new helper,
errorDetail, is used wherever a non-JSON body becomes an error's detail, including when the body is JSON but not an error for that status:<!DOCTYPE htmlor<html: just its<title>, with entities decoded and whitespace collapsed. So the Cloudflare page becomes504 Gateway Timeout: api.openai.com | 504: Gateway time-out. A page without a title gives just the status.…, at the start of a character.Plain-text errors, which the existing
Test_Do_NonOK_WithBodycovers, come through as before.