fix(api): validate HTTP status and add image download timeout#1
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. To trigger a review, include ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches✨ Simplify code
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/api/client_test.go (1)
41-43: ⚡ Quick winStrengthen the 400 test to prove API-error precedence (not just message presence).
At Line 41,
strings.Contains(err.Error(), "bad input")can still pass if behavior regresses to the fallbackAPI returned HTTP 400: ...path, because that string may include"bad input"too.Suggested assertion tightening
if !strings.Contains(err.Error(), "bad input") { t.Errorf("expected error to surface API error message, got: %v", err) } + if strings.Contains(err.Error(), "API returned HTTP 400") { + t.Fatalf("expected API error precedence over HTTP fallback, got: %v", err) + }🤖 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 `@internal/api/client_test.go` around lines 41 - 43, The current test only checks strings.Contains(err.Error(), "bad input") which could still pass if the error is the fallback "API returned HTTP 400: ..."; update the assertion on err to prove the API's error string takes precedence by (1) asserting err.Error() does not contain the fallback prefix "API returned HTTP 400" and (2) asserting the error message exactly equals (or at least starts/contains only) the API-provided message "bad input" instead of relying on a loose contains check; locate the check using the err variable and the existing strings.Contains call and replace it with the two stronger assertions.
🤖 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.
Nitpick comments:
In `@internal/api/client_test.go`:
- Around line 41-43: The current test only checks strings.Contains(err.Error(),
"bad input") which could still pass if the error is the fallback "API returned
HTTP 400: ..."; update the assertion on err to prove the API's error string
takes precedence by (1) asserting err.Error() does not contain the fallback
prefix "API returned HTTP 400" and (2) asserting the error message exactly
equals (or at least starts/contains only) the API-provided message "bad input"
instead of relying on a loose contains check; locate the check using the err
variable and the existing strings.Contains call and replace it with the two
stronger assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 245920c3-1427-41ec-b12d-84ffb5bf7153
📒 Files selected for processing (3)
cmd/image_inference.gointernal/api/client.gointernal/api/client_test.go
- internal/api/client.go: surface non-2xx responses as errors, even when the body isn't a parseable APIResponse. API-level error objects still win when present so existing typed errors keep flowing. - cmd/image_inference.go: replace http.DefaultClient with a 5-minute client so a hung CDN can't stall the CLI indefinitely. - internal/api/client_test.go: cover 5xx without errors body, 4xx with errors body, and 401 invalidApiKey.
74a8776 to
db6c35b
Compare
…abling robust cancelation and propagation
There was a problem hiding this comment.
Pull request overview
This PR improves CLI robustness by (a) surfacing HTTP-level failures from the Runware API client and (b) introducing timeout controls for API calls and result downloads, plus adding API client tests.
Changes:
- Update
internal/apiclient error handling to prioritize APIerrors[]but also surface non-2xx HTTP responses when the body can’t be parsed as anAPIResponse. - Add
--request-timeout(persistent) and wire command contexts through API calls; add--download-timeoutfor image result downloads. - Add
internal/api/client_test.gocovering several error-response scenarios.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/api/client.go | Adjust API response parsing / error surfacing; add response-body truncation helper. |
| internal/api/client_test.go | Add tests for non-2xx responses with/without API error bodies and unauthorized behavior. |
| cmd/root.go | Add --request-timeout and contextWithTimeout helper used across commands. |
| cmd/common.go | Define default request/download timeout constants. |
| cmd/image_inference.go | Use timeout-derived contexts for API calls; add per-image download timeout wrapper. |
| cmd/video_inference.go | Switch API calls/polling and downloads to use the timeout-derived context. |
| cmd/audio_inference.go | Switch API calls/polling and downloads to use the timeout-derived context. |
| cmd/text_inference.go | Use timeout-derived context for API calls. |
| cmd/ping.go | Use timeout-derived context for API calls. |
| cmd/model_search.go | Use timeout-derived context for API calls. |
| cmd/auth.go | Use timeout-derived context for API calls (key validation/status). |
| cmd/account.go | Use timeout-derived context for API calls (account details). |
Comments suppressed due to low confidence (1)
cmd/image_inference.go:363
- This still uses
http.DefaultClientfor downloads, and the only timeout comes from the request context. The PR description mentions replacinghttp.DefaultClientwith a timeout-bound client; either implement a dedicatedhttp.Client{Timeout: ...}here (and decide whether it’s per-image or global), or update the PR description to match the context-based timeout approach.
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…evel, enabling robust cancelation and propagation" This reverts commit a3b71b0.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Summary
internal/api/client.go, even when the body isn't a parseableAPIResponse. API-level error objects still take precedence, so existing typed errors (e.g.ErrUnauthorized) keep flowing.internal/api/client_test.gocovering 5xx without errors body, 4xx with errors body, and 401invalidApiKey.Test plan
go test ./...make lint🤖 Generated with Claude Code