fix: surface non-OK response bodies instead of discarding them#64
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughNon-OK fetch responses now preserve capped response-body diagnostics. ChangesError diagnostics
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/test/fetch-402-recovery.test.ts (1)
153-155: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAlso assert content alongside payment recovery.
This verifies the unpaid branch only. Add an assertion in the paid non-OK case that
error.details.contentis present together withpaymentRecovery, ensuring both spreads remain intact.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/test/fetch-402-recovery.test.ts` around lines 153 - 155, Extend the paid non-OK test case in fetch-402-recovery.test.ts to assert that error.details includes both paymentRecovery and the preserved content field. Keep the existing unpaid-branch assertion unchanged and verify content alongside the recovery data.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/tools/lightning/fetch.ts`:
- Around line 119-124: Update the non-OK response handling in the fetch flow to
read result.body through a bounded stream reader before passing the content to
errorBody, rather than calling result.text(). Enforce the established
response-size cap, stop reading once it is reached, and preserve the
content_truncated indicator in the resulting DetailedError metadata while
retaining paidRecoveryDetails handling.
---
Nitpick comments:
In `@src/test/fetch-402-recovery.test.ts`:
- Around line 153-155: Extend the paid non-OK test case in
fetch-402-recovery.test.ts to assert that error.details includes both
paymentRecovery and the preserved content field. Keep the existing unpaid-branch
assertion unchanged and verify content alongside the recovery data.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 365a34e6-7658-4096-866a-6007bdf36ffe
📒 Files selected for processing (3)
src/test/fetch-402-recovery.test.tssrc/test/fetch-dry-run.test.tssrc/tools/lightning/fetch.ts
…2 headers Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Fixes #63
Non-OK responses discarded their body, which is often the only diagnostic - e.g. the l402.space gateway's 502 explaining exactly why it refused ("upstream price $4.78 exceeds this gateway's per-request cap") was reduced to
{"status": 502, "payment_required": false}, forcing a drop to raw curl to see the reason.Changes
Both fetch paths now surface the error body, structurally and capped at 4096 chars (with
content_truncated: truewhen cut off), via a sharedreadErrorBodyhelper:--dry-run: a non-2xx, non-402 response now includescontentalongsidestatus/payment_required(empty bodies are omitted; 2xx stays body-less - a dry run is about the price, not the content)contentas a structured field next topaymentRecovery(when a payment was made), instead of embedding the unbounded body inside the error message stringReview follow-ups
text()before slicing - an arbitrarily large error body is never held in memory. A failed read mid-body surfaces what was read so far instead of throwing, so thepaymentRecoverydetails next to it are never lost--dry-runno longer regex-scans the 402 response body for a BOLT11 invoice. The pay path in lightning-tools only ever reads invoices from headers (WWW-Authenticatefor L402/MPP,Payment-Requiredfor x402), so a body-only invoice was a price preview the fetch could never actually pay. Those responses now fall through to the l402.space bridge suggestion, and dropping the fallback lets the dry-run body read be bounded tooVerification
New tests: non-2xx body surfaced, 4096-char truncation flagged, empty body omitted, and the non-OK-without-payment error now asserts its structured
content; full suite passes (133 tests). Verified live against a bridged 404: both--dry-runand normal fetch now show the upstream error page that was previously discarded.🤖 Generated with Claude Code
Summary by CodeRabbit