Skip to content

Commit 0624bf2

Browse files
committed
refactor: [torrust#649] use anyhow to handle errors
in the HTTP tracker client.
1 parent 271bfa8 commit 0624bf2

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/bin/http_tracker_client.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! ```
1616
use std::str::FromStr;
1717

18+
use anyhow::Context;
1819
use clap::{Parser, Subcommand};
1920
use reqwest::Url;
2021
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
@@ -37,24 +38,25 @@ enum Command {
3738
}
3839

3940
#[tokio::main]
40-
async fn main() {
41+
async fn main() -> anyhow::Result<()> {
4142
let args = Args::parse();
4243

4344
match args.command {
4445
Command::Announce { tracker_url, info_hash } => {
45-
announce_command(tracker_url, info_hash).await;
46+
announce_command(tracker_url, info_hash).await?;
4647
}
4748
Command::Scrape {
4849
tracker_url,
4950
info_hashes,
5051
} => {
51-
scrape_command(&tracker_url, &info_hashes).await;
52+
scrape_command(&tracker_url, &info_hashes).await?;
5253
}
5354
}
55+
Ok(())
5456
}
5557

56-
async fn announce_command(tracker_url: String, info_hash: String) {
57-
let base_url = Url::parse(&tracker_url).expect("Invalid HTTP tracker base URL");
58+
async fn announce_command(tracker_url: String, info_hash: String) -> anyhow::Result<()> {
59+
let base_url = Url::parse(&tracker_url).context("failed to parse HTTP tracker base URL")?;
5860
let info_hash =
5961
InfoHash::from_str(&info_hash).expect("Invalid infohash. Example infohash: `9c38422213e30bff212b30c360d26f9a02136422`");
6062

@@ -67,16 +69,17 @@ async fn announce_command(tracker_url: String, info_hash: String) {
6769
let announce_response: Announce = serde_bencode::from_bytes(&body)
6870
.unwrap_or_else(|_| panic!("response body should be a valid announce response, got: \"{:#?}\"", &body));
6971

70-
let json = serde_json::to_string(&announce_response).expect("announce response should be a valid JSON");
72+
let json = serde_json::to_string(&announce_response).context("failed to serialize scrape response into JSON")?;
7173

7274
println!("{json}");
75+
76+
Ok(())
7377
}
7478

75-
async fn scrape_command(tracker_url: &str, info_hashes: &[String]) {
76-
let base_url = Url::parse(tracker_url).expect("Invalid HTTP tracker base URL");
79+
async fn scrape_command(tracker_url: &str, info_hashes: &[String]) -> anyhow::Result<()> {
80+
let base_url = Url::parse(tracker_url).context("failed to parse HTTP tracker base URL")?;
7781

78-
let query = requests::scrape::Query::try_from(info_hashes)
79-
.expect("All infohashes should be valid. Example infohash: `9c38422213e30bff212b30c360d26f9a02136422`");
82+
let query = requests::scrape::Query::try_from(info_hashes).context("failed to parse infohashes")?;
8083

8184
let response = Client::new(base_url).scrape(&query).await;
8285

@@ -85,7 +88,9 @@ async fn scrape_command(tracker_url: &str, info_hashes: &[String]) {
8588
let scrape_response = scrape::Response::try_from_bencoded(&body)
8689
.unwrap_or_else(|_| panic!("response body should be a valid scrape response, got: \"{:#?}\"", &body));
8790

88-
let json = serde_json::to_string(&scrape_response).expect("scrape response should be a valid JSON");
91+
let json = serde_json::to_string(&scrape_response).context("failed to serialize scrape response into JSON")?;
8992

9093
println!("{json}");
94+
95+
Ok(())
9196
}

src/shared/bit_torrent/tracker/http/client/requests/scrape.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::convert::TryFrom;
2+
use std::error::Error;
23
use std::fmt::{self};
34
use std::str::FromStr;
45

@@ -16,8 +17,17 @@ impl fmt::Display for Query {
1617
}
1718

1819
#[derive(Debug)]
20+
#[allow(dead_code)]
1921
pub struct ConversionError(String);
2022

23+
impl fmt::Display for ConversionError {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
write!(f, "Invalid infohash: {}", self.0)
26+
}
27+
}
28+
29+
impl Error for ConversionError {}
30+
2131
impl TryFrom<&[String]> for Query {
2232
type Error = ConversionError;
2333

0 commit comments

Comments
 (0)