Skip to content

Shorten error bodies which aren't JSON to an HTML page's title, or 512 bytes - #77

Merged
djthorpe merged 2 commits into
mainfrom
djt/1026/error-bodies
Oct 26, 2026
Merged

djthorpe merged 2 commits into
mainfrom
djt/1026/error-bodies

Conversation

@djthorpe

@djthorpe djthorpe commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

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.com filled 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:

  • An HTML page, by its content type or by starting with <!DOCTYPE html or <html: just its <title>, with entities decoded and whitespace collapsed. So the Cloudflare page becomes 504 Gateway Timeout: api.openai.com | 504: Gateway time-out. A page without a title gives just the status.
  • Other text: trimmed of surrounding space, and cut at 512 bytes with …, at the start of a character.

Plain-text errors, which the existing Test_Do_NonOK_WithBody covers, come through as before.

…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>
Copilot AI balanced review requested due to automatic review settings October 26, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread client.go Resolved
@djthorpe djthorpe self-assigned this Oct 26, 2026
- 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>
@djthorpe
djthorpe merged commit 8a243bd into main Oct 26, 2026
4 checks passed
@djthorpe
djthorpe deleted the djt/1026/error-bodies branch October 26, 2026 17:42
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