Skip to content

fix: surface HTTP errors from the web API client - #28

Merged
ayoubfaouzi merged 1 commit into
mainfrom
fix/webapi-error-handling
Aug 3, 2026
Merged

fix: surface HTTP errors from the web API client#28
ayoubfaouzi merged 1 commit into
mainfrom
fix/webapi-error-handling

Conversation

@ayoubfaouzi

@ayoubfaouzi ayoubfaouzi commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

Second item from the improvement backlog: several internal/webapi methods never checked the HTTP response status.

Before this PR:

  • Rescan returned nil on 401/404/500 — the user thought the rescan was queued when it wasn't.
  • Download returned whatever body came back, so a JSON error message could get written to disk as the "sample".
  • Delete and GetFile ignored the status code entirely.
  • FileExists treated any non-404 response (including 401 and 500) as "the file exists".
  • ListFiles did an unchecked jsonBody["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 form HTTP <code>: <api message>, preferring the API's own message field 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

  • New regression tests: TestRescanError, TestDownloadError (asserts the error body is not returned as a buffer), TestDeleteError, TestGetFileError, TestFileExistsServerError.
  • Two existing assertions updated for the new HTTP <code>: error prefix.
  • go build ./... && go vet ./... && go test -race ./... passes locally.

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.
@ayoubfaouzi
ayoubfaouzi merged commit 99941eb into main Aug 3, 2026
3 checks passed
@ayoubfaouzi
ayoubfaouzi deleted the fix/webapi-error-handling branch August 3, 2026 13:08
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.

1 participant