Skip to content

SI-8: Eliminate all unwraps from the REST API client package #1969

Description

@josecelano

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:

  1. 11 public ApiHttpClient methods — thin wrappers that delegate to fallible *_result() counterparts but .unwrap() the result.
  2. post_empty(), post_form(), delete() (private) — same wrapper-with-unwrap pattern.
  3. get() (pub method on ApiHttpClient) — same pattern.
  4. get() (pub free function) — thin wrapper around get_result().
  5. 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:

  1. headers_with_request_id()Uuid::to_string() always produces a valid ASCII string, and HeaderValue::from_str() for ASCII strings never fails.
  2. headers_with_auth_token() — same pattern, pre-formatted token string.
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions