fix: surface HTTP errors from the web API client - #28
Merged
Conversation
Rescan, Download, Delete, and GetFile silently ignored the response status code: a failed rescan reported success, and a download error body would be saved to disk as the sample. FileExists also treated any non-404 response (including 401/500) as the file existing, and ListFiles could panic on an error body without a message field. Route all requests through a shared do() helper that reads the body and converts any non-2xx response into an error, preferring the API's own message field and falling back to the (truncated) raw body. Add regression tests for every error path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Second item from the improvement backlog: several
internal/webapimethods never checked the HTTP response status.Before this PR:
Rescanreturnednilon 401/404/500 — the user thought the rescan was queued when it wasn't.Downloadreturned whatever body came back, so a JSON error message could get written to disk as the "sample".DeleteandGetFileignored the status code entirely.FileExiststreated any non-404 response (including 401 and 500) as "the file exists".ListFilesdid an uncheckedjsonBody["message"].(string)type assertion — a panic if the error body had a different shape.The fix: all requests now go through a shared
do()helper that reads the body and converts any non-2xx response into an error of the formHTTP <code>: <api message>, preferring the API's ownmessagefield and falling back to the truncated raw body. This also removed the copy-pasted read-body/close boilerplate from every method.Method signatures are unchanged, so all callers (TUI models, view/search commands) keep working — they now just receive real errors instead of false successes.
Test plan
TestRescanError,TestDownloadError(asserts the error body is not returned as a buffer),TestDeleteError,TestGetFileError,TestFileExistsServerError.HTTP <code>:error prefix.go build ./... && go vet ./... && go test -race ./...passes locally.