Description
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
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 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, 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
| 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
Activity
refactor(rest-api-client): eliminate all unwraps from the REST API cl…
refactor(rest-api-client): eliminate all unwraps from the REST API cl…
Merge #1973: SI-8: Eliminate all unwraps from the REST API client pac…
chore(issues): archive closed issue specs torrust#1447, torrust#1459, t…
Merge #1984: chore(issues): archive 10 closed issue specs from docs/i…