fix(api): handle body read error and cap response size in FormatHTTPError#252
Conversation
…rror Address review feedback: stop discarding the io.ReadAll error so I/O failures surface in the error message, and wrap the body reader with LimitReader(1 MiB) to guard against oversized error responses from proxies. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Pull request overview
This PR updates util.FormatHTTPError to be safer and more informative when formatting errors from non-2xx HTTP responses, addressing prior review feedback from #250 around unbounded reads and discarded read errors.
Changes:
- Cap buffered error-body reads to 1 MiB via
io.LimitReaderto avoid unbounded memory usage. - Surface
io.ReadAllfailures as a wrapped error instead of silently falling back to a status-only message.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // action is a short verb phrase describing what failed, e.g. "create upload session". | ||
| func FormatHTTPError(action string, resp *http.Response) error { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) |
| body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: failed to read response body (status: %d): %w", action, resp.StatusCode, err) | ||
| } |
PR #252 — Synthesis (Gandalf, Aggregator)Overall Verdict: APPROVE (4 approve, 1 request-changes — consensus is approve)Reviewers: Legolas (request changes), Gimli (approve), Sam (approve), Merry (approve), Frodo (approve) Summary of ChangesThis PR hardens
All 4 production call sites in upload paths benefit without per-caller changes. Findings (Deduplicated)🔴 CRITICALNone. 🟠 MAJOR
🟡 NIT
🟢 INFO
Recommended Labels
Synthesized by Gandalf from 5/7 reviews. |
Description
Follow-up to #250. The review-feedback commit addressing CodeRabbit's
io.ReadAllfinding was accidentally pushed to a branch onzeabur/cliinstead of the fork branch backing the PR, so #250 merged without it. This PR lands that commit on main.Change
In
util.FormatHTTPError:io.ReadAllerror — I/O failures during body read now surface asfailed to read response body (status: %d): <err>instead of silently degrading to the bare status-code message.io.LimitReader(resp.Body, 1<<20)to cap error-body buffering at 1 MiB, guarding against oversized error pages from proxies.Addresses the review threads on #250 from CodeRabbit ("Handle the error from
io.ReadAll", Major) and Copilot (unbounded read), plus the zeabur-review-agentLimitReadersuggestion.Verification
go build ./...cleango test ./pkg/util/...pass (existingTestFormatHTTPErrormatrix still green)code, HTML proxy error, S3 XML error, empty body, unicode message, 2 MB body truncated at 1 MiBRelated issues & labels
🤖 Generated with Claude Code
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.Summary by CodeRabbit