Subissue of #1938
Goal
Eliminate all .unwrap() calls from the torrust-tracker-rest-api-client package. Every operation that can fail must return a Result. For operations that are provably infallible, replace bare .unwrap() with an explicit .expect("infallible: ...") that documents why the operation cannot fail.
Background
The ApiClient was made fully panic-free in SI-6 (PR #1968). However, the low-level ApiHttpClient and several free functions/helpers in client.rs still contain .unwrap() and .expect() calls that can panic at runtime.
Transport unwraps (must return Result)
These are real failure points — network errors, URL parsing failures, etc. They must return Result:
- 11 public
ApiHttpClient methods — thin wrappers that delegate to fallible *_result() counterparts but .unwrap() the result.
post_empty(), post_form(), delete() (private) — same wrapper-with-unwrap pattern.
get() (pub method on ApiHttpClient) — same pattern.
get() (pub free function) — thin wrapper around get_result().
get_request() (pub on ApiHttpClient) — calls base_url() which already returns Result.
Infallible conversions (replace unwrap with expect)
These are provably infallible operations where a descriptive expect message is the right pattern:
headers_with_request_id() — Uuid::to_string() always produces a valid ASCII string, and HeaderValue::from_str() for ASCII strings never fails.
headers_with_auth_token() — same pattern, pre-formatted token string.
get_request_with_query_result() auth token inserts — 2 token-to-HeaderValue conversions, same provably-infallible pattern.
Implementation Plan
| ID |
Status |
Task |
Notes |
| T1 |
TODO |
Move ApiHttpClient public methods to return Result |
10 methods + get() method — return ClientError |
| T2 |
TODO |
Update all callers in contract tests |
~65 ApiHttpClient::new(...) call sites |
| T3 |
TODO |
Update callers in E2E test wrapper |
src/console/ci/qbittorrent_e2e/tracker/client.rs |
| T4 |
TODO |
Update callers in integration tests |
tests/servers/api/contract/stats/mod.rs |
| T5 |
TODO |
Replace bare .unwrap() with .expect("infallible: ...") for provably infallible conversions |
headers_with_request_id, headers_with_auth_token, auth token inserts |
| T6 |
TODO |
Verify pre-commit and pre-push checks pass |
|
Full spec: docs/issues/open/1967-1938-si-8-eliminate-unwraps-from-rest-api-client.md
Subissue of #1938
Goal
Eliminate all
.unwrap()calls from thetorrust-tracker-rest-api-clientpackage. Every operation that can fail must return aResult. For operations that are provably infallible, replace bare.unwrap()with an explicit.expect("infallible: ...")that documents why the operation cannot fail.Background
The
ApiClientwas made fully panic-free in SI-6 (PR #1968). However, the low-levelApiHttpClientand several free functions/helpers inclient.rsstill contain.unwrap()and.expect()calls that can panic at runtime.Transport unwraps (must return
Result)These are real failure points — network errors, URL parsing failures, etc. They must return
Result:ApiHttpClientmethods — thin wrappers that delegate to fallible*_result()counterparts but.unwrap()the result.post_empty(),post_form(),delete()(private) — same wrapper-with-unwrap pattern.get()(pub method onApiHttpClient) — same pattern.get()(pub free function) — thin wrapper aroundget_result().get_request()(pub onApiHttpClient) — callsbase_url()which already returnsResult.Infallible conversions (replace
unwrapwithexpect)These are provably infallible operations where a descriptive
expectmessage is the right pattern:headers_with_request_id()—Uuid::to_string()always produces a valid ASCII string, andHeaderValue::from_str()for ASCII strings never fails.headers_with_auth_token()— same pattern, pre-formatted token string.get_request_with_query_result()auth token inserts — 2 token-to-HeaderValue conversions, same provably-infallible pattern.Implementation Plan
ApiHttpClientpublic methods to returnResultget()method — returnClientErrorApiHttpClient::new(...)call sitessrc/console/ci/qbittorrent_e2e/tracker/client.rstests/servers/api/contract/stats/mod.rs.unwrap()with.expect("infallible: ...")for provably infallible conversionsheaders_with_request_id,headers_with_auth_token, auth token insertsFull spec:
docs/issues/open/1967-1938-si-8-eliminate-unwraps-from-rest-api-client.md