Skip to content

fix(opencode): show human-readable message for HTML error responses#15407

Open
rianvdm wants to merge 1 commit intoanomalyco:devfrom
rianvdm:fix/html-error-message
Open

fix(opencode): show human-readable message for HTML error responses#15407
rianvdm wants to merge 1 commit intoanomalyco:devfrom
rianvdm:fix/html-error-message

Conversation

@rianvdm
Copy link

@rianvdm rianvdm commented Feb 27, 2026

Issue for this PR

Closes #15406

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When OpenCode is behind a gateway or proxy that returns HTML error pages (e.g. Cloudflare Access returning a 401), the error handler in packages/opencode/src/provider/error.ts dumps raw HTML into the terminal because JSON.parse fails on the response body and it falls through to return \${msg}: ${e.responseBody}``.

This adds a check after the JSON parse attempt: if the response body starts with <!doctype or <html>, return a clean message instead of the markup. For 401 it tells the user to run opencode auth login, for 403 it suggests checking account/provider settings, and for other status codes it returns the status text alone.

The regex only matches HTML — JSON error responses are completely unaffected.

How did you verify your code works?

Reproduced the issue with a Cloudflare Access-protected provider endpoint where an expired token produces an HTML 401 response. Confirmed the new code path triggers and displays the clean message instead of raw HTML. Also confirmed that normal JSON API errors (e.g. from OpenAI, Anthropic) are unaffected since they don't match the HTML regex.

Screenshots / recordings

Before:

Unauthorized: <html>
<head><title>401 Unauthorized</title></head>
<body>
<center><h1>401 Unauthorized</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

After:

Unauthorized: request was blocked by a gateway or proxy. Your authentication token may be missing or expired — try running `opencode auth login <your provider URL>` to re-authenticate.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When a gateway or proxy (e.g. Cloudflare Access) returns an HTML error
page instead of JSON, the error handler was dumping raw HTML markup into
the terminal. This detects HTML response bodies and substitutes a
helpful, actionable message instead.
@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Feb 27, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

HTML error pages from gateways/proxies produce unreadable terminal output

1 participant