Skip to content

Fix: Sanitize HTML error responses in request() to improve Sentry reporting#122

Open
gumclaw wants to merge 2 commits intomainfrom
fix/sanitize-request-error-messages
Open

Fix: Sanitize HTML error responses in request() to improve Sentry reporting#122
gumclaw wants to merge 2 commits intomainfrom
fix/sanitize-request-error-messages

Conversation

@gumclaw
Copy link
Copy Markdown

@gumclaw gumclaw commented Mar 28, 2026

Problem

When the API returns a non-OK response with an HTML body (e.g. a 404 "Page not found" page), the entire HTML gets stuffed into the Error message. This causes:

  1. Sentry to misparse stacktraces (HTML tags appear as function names)
  2. Error grouping to break
  3. Noisy, unreadable error messages

Sentry issue: https://gumroad-to.sentry.io/issues/7369975611/

Solution

Detect HTML error responses via:

  • <!DOCTYPE or <html prefix in the response body
  • text/html in the Content-Type header

When HTML is detected, extract just the <title> text (e.g. "Page not found"), falling back to "HTML error page". Plain-text errors are truncated to 200 chars for cleaner Sentry reporting.

The full raw error body (up to 500 chars) is still logged to console.info for debugging.

Tests

Added 6 tests covering: success path, 401, plain-text truncation, HTML title extraction, HTML without title fallback, and content-type detection.

…orting

When the API returns a non-OK response with an HTML body (e.g. a 404 page),
the entire HTML gets stuffed into the Error message. This causes Sentry to
misparse stacktraces and breaks error grouping.

Now we detect HTML responses (via DOCTYPE/html prefix or content-type header)
and extract just the <title> text, falling back to 'HTML error page'. Plain
text errors are truncated to 200 chars. The full raw error (up to 500 chars)
is still logged to console for debugging.
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