Skip to content

Refactor: bit_torrent::tracker::http::client::Client to return errors instead of panicking #680

@josecelano

Description

@josecelano

The HTTP Tracker Client struct contains some unwrap calls.

pub struct Client {
    base_url: Url,
    reqwest: ReqwestClient,
    key: Option<Key>,
}

impl Client {
    // ...

    pub fn bind(base_url: Url, local_address: IpAddr) -> Self {
        Self {
            base_url,
            reqwest: reqwest::Client::builder().local_address(local_address).build().unwrap(),
            key: None,
        }
    }

    // ...

    pub fn authenticated(base_url: Url, key: Key) -> Self {
        Self {
            base_url,
            reqwest: reqwest::Client::builder().build().unwrap(),
            key: Some(key),
        }
    }

    // ...

    pub async fn get(&self, path: &str) -> Response {
        self.reqwest.get(self.build_url(path)).send().await.unwrap()
    }

    // ...

    pub async fn get_with_header(&self, path: &str, key: &str, value: &str) -> Response {
        self.reqwest
            .get(self.build_url(path))
            .header(key, value)
            .send()
            .await
            .unwrap()
    }

    // ...
}

That makes it impossible to catch those errors upstream. For example, the Tracker Checker can not use this Client and continue its execution if an HTTP Tracker request fails. See, for example, this issue: #678.

We should return the Result type and let the caller handle the problem.

This Client was introduced for testing purposes and later moved to production, so it lacks some important features like:

  • Testing.
  • Better error handling.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions