Skip to main content

fx_torrent/tracker/
error.rs

1use crate::tracker::TrackerHandle;
2use crate::{bencode, InfoHash};
3use std::io;
4use thiserror::Error;
5use tokio::time::error::Elapsed;
6use url::{ParseError, Url};
7
8/// The result type of tracker operations.
9pub type Result<T> = std::result::Result<T, TrackerError>;
10
11/// Error type for tracker-related operations.
12///
13/// This enum groups all errors that can occur while configuring trackers,
14/// connecting to them, or parsing their responses.
15#[derive(Debug, Error)]
16pub enum TrackerError {
17    #[error("the tracker handle {0} is invalid")]
18    InvalidHandle(TrackerHandle),
19    #[error("tracker url is invalid, {0}")]
20    InvalidUrl(String),
21    #[error("port {0} is invalid")]
22    InvalidPort(u16),
23    #[error("info hash {0} not found within tracker")]
24    InfoHashNotFound(InfoHash),
25    #[error("tracker has no available addresses (left)")]
26    Unavailable,
27    #[error("an error occurred while connecting to the tracker, {0}")]
28    Connection(String),
29    #[error("tracker scheme \"{0}\" is not supported")]
30    UnsupportedScheme(String),
31    #[error("announcement event \"{0}\" is not supported")]
32    UnsupportedEvent(String),
33    #[error("failed to announce to the tracker, {0}")]
34    AnnounceError(String),
35    #[error("an io error occurred while communicating with the tracker, {0}")]
36    Io(io::Error),
37    #[error("failed to parse message, {0}")]
38    Parse(String),
39    #[error("tracker url \"{0}\" is already registered")]
40    DuplicateUrl(Url),
41    #[error("unable to execute the operation, no active trackers available")]
42    NoTrackers,
43}
44
45impl PartialEq for TrackerError {
46    fn eq(&self, other: &Self) -> bool {
47        match (self, other) {
48            (Self::InvalidHandle(_), Self::InvalidHandle(_)) => true,
49            (Self::InvalidUrl(_), Self::InvalidUrl(_)) => true,
50            (Self::InvalidPort(_), Self::InvalidPort(_)) => true,
51            (Self::InfoHashNotFound(_), Self::InfoHashNotFound(_)) => true,
52            (Self::Unavailable, Self::Unavailable) => true,
53            (Self::Connection(_), Self::Connection(_)) => true,
54            (Self::UnsupportedScheme(_), Self::UnsupportedScheme(_)) => true,
55            (Self::UnsupportedEvent(_), Self::UnsupportedEvent(_)) => true,
56            (Self::AnnounceError(_), Self::AnnounceError(_)) => true,
57            (Self::Io(_), Self::Io(_)) => true,
58            (Self::Parse(_), Self::Parse(_)) => true,
59            (Self::DuplicateUrl(_), Self::DuplicateUrl(_)) => true,
60            (Self::NoTrackers, Self::NoTrackers) => true,
61            _ => false,
62        }
63    }
64}
65
66impl From<io::Error> for TrackerError {
67    fn from(e: io::Error) -> Self {
68        Self::Io(e)
69    }
70}
71
72impl From<Elapsed> for TrackerError {
73    fn from(err: Elapsed) -> Self {
74        Self::Io(io::Error::new(io::ErrorKind::TimedOut, err.to_string()))
75    }
76}
77
78impl From<reqwest::Error> for TrackerError {
79    fn from(err: reqwest::Error) -> Self {
80        Self::Io(io::Error::new(io::ErrorKind::Other, err.to_string()))
81    }
82}
83
84impl From<ParseError> for TrackerError {
85    fn from(error: ParseError) -> Self {
86        Self::InvalidUrl(error.to_string())
87    }
88}
89
90impl From<bencode::Error> for TrackerError {
91    fn from(error: bencode::Error) -> Self {
92        Self::Parse(error.to_string())
93    }
94}