Skip to main content

fx_torrent/
error.rs

1#[cfg(feature = "dht")]
2use crate::dht;
3use crate::tracker::TrackerError;
4use crate::{bencode, peer, TorrentHandle};
5use std::io;
6use thiserror::Error;
7
8/// The result type for the torrent package.
9pub type Result<T> = std::result::Result<T, TorrentError>;
10
11/// Represents possible errors that can occur when parsing a magnet URI.
12pub type MagnetResult<T> = std::result::Result<T, MagnetError>;
13
14/// Represents possible errors that can occur when parsing a magnet URI.
15#[derive(Debug, Clone, Error, PartialEq)]
16pub enum MagnetError {
17    /// Failed to parse the magnet URI.
18    #[error("failed to parse magnet uri, {0}")]
19    Parse(String),
20    /// The specified magnet URI is invalid.
21    #[error("invalid magnet uri")]
22    InvalidUri,
23    /// The specified file index value is invalid.
24    #[error("value \"{0}\" is invalid")]
25    InvalidValue(String),
26    /// The specified scheme in the magnet URI is not supported.
27    #[error("scheme \"{0}\" is not supported")]
28    UnsupportedScheme(String),
29}
30
31/// The torrent piece specific errors.
32/// These errors can occur when working with [Piece] related operations.
33#[derive(Debug, Clone, Error, PartialEq)]
34pub enum PieceError {
35    #[error("torrent pieces are currently unknown")]
36    Unavailable,
37    #[error("failed to calculate pieces, {0}")]
38    UnableToDeterminePieces(String),
39    #[error("exceeding chunk size, expected size {0} but got {1}")]
40    InvalidChunkSize(usize, usize),
41}
42
43#[derive(Debug, Error)]
44pub enum TorrentError {
45    #[error("failed to parse magnet uri, {0}")]
46    Magnet(MagnetError),
47    #[error("failed to parse torrent data, {0}")]
48    TorrentParse(String),
49    #[error("failed to parse address, {0}")]
50    AddressParse(String),
51    #[error("the metadata of the torrent is invalid, {0}")]
52    InvalidMetadata(String),
53    #[error("the provided exact topic (xt) is invalid, {0}")]
54    InvalidTopic(String),
55    #[error("the provided info hash is invalid, {0}")]
56    InvalidInfoHash(String),
57    #[error("torrent handle {0} is no longer valid or invalid")]
58    InvalidHandle(TorrentHandle),
59    #[error("torrent request is invalid, {0}")]
60    InvalidRequest(String),
61    #[error("session is invalid, {0}")]
62    InvalidSession(String),
63    #[error("the specified range {0:?} is invalid")]
64    InvalidRange(std::ops::Range<usize>),
65    #[error("tracker error: {0}")]
66    Tracker(TrackerError),
67    #[error("peer error: {0}")]
68    Peer(peer::Error),
69    #[cfg(feature = "dht")]
70    #[error("dht error: {0}")]
71    Dht(dht::Error),
72    #[error("an io error occurred, {0}")]
73    Io(io::Error),
74    #[error("a torrent piece error occurred, {0}")]
75    Piece(PieceError),
76    #[error("the requested data is unavailable")]
77    DataUnavailable,
78}
79
80impl PartialEq for TorrentError {
81    fn eq(&self, other: &Self) -> bool {
82        match (self, other) {
83            (Self::Magnet(_), Self::Magnet(_)) => true,
84            (Self::TorrentParse(_), Self::TorrentParse(_)) => true,
85            (Self::AddressParse(_), Self::AddressParse(_)) => true,
86            (Self::InvalidMetadata(_), Self::InvalidMetadata(_)) => true,
87            (Self::InvalidTopic(_), Self::InvalidTopic(_)) => true,
88            (Self::InvalidInfoHash(le), Self::InvalidInfoHash(re)) => le == re,
89            (Self::InvalidHandle(_), Self::InvalidHandle(_)) => true,
90            (Self::InvalidRequest(_), Self::InvalidRequest(_)) => true,
91            (Self::InvalidSession(_), Self::InvalidSession(_)) => true,
92            (Self::InvalidRange(_), Self::InvalidRange(_)) => true,
93            (Self::Tracker(le), Self::Tracker(re)) => le == re,
94            (Self::Peer(le), Self::Peer(re)) => le == re,
95            #[cfg(feature = "dht")]
96            (Self::Dht(le), Self::Dht(re)) => le == re,
97            (Self::Io(_), Self::Io(_)) => true,
98            (Self::Piece(_), Self::Piece(_)) => true,
99            (Self::DataUnavailable, Self::DataUnavailable) => true,
100            _ => false,
101        }
102    }
103}
104
105impl From<MagnetError> for TorrentError {
106    fn from(err: MagnetError) -> Self {
107        TorrentError::Magnet(err)
108    }
109}
110
111impl From<TrackerError> for TorrentError {
112    fn from(error: TrackerError) -> Self {
113        Self::Tracker(error)
114    }
115}
116
117impl From<peer::Error> for TorrentError {
118    fn from(error: peer::Error) -> Self {
119        Self::Peer(error)
120    }
121}
122
123impl From<io::Error> for TorrentError {
124    fn from(err: io::Error) -> Self {
125        Self::Io(err)
126    }
127}
128
129impl From<bencode::Error> for TorrentError {
130    fn from(error: bencode::Error) -> Self {
131        Self::TorrentParse(error.to_string())
132    }
133}
134
135impl From<PieceError> for TorrentError {
136    fn from(error: PieceError) -> Self {
137        Self::Piece(error)
138    }
139}
140
141#[cfg(feature = "dht")]
142impl From<dht::Error> for TorrentError {
143    fn from(error: dht::Error) -> Self {
144        Self::Dht(error)
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151    use std::io;
152
153    #[test]
154    fn test_torrent_error_from_magnet_error() {
155        let err_text = "lorem ipsum dolor";
156        let err = MagnetError::Parse(err_text.to_string());
157        let expected_result = TorrentError::Magnet(err.clone());
158
159        let result: TorrentError = err.into();
160
161        assert_eq!(expected_result, result)
162    }
163
164    #[test]
165    fn test_torrent_error_from_tracker_error() {
166        let err_text = "foo bar";
167        let err = TrackerError::Connection(err_text.to_string());
168        let expected_result = TorrentError::Tracker(TrackerError::Connection(err_text.to_string()));
169
170        let result: TorrentError = err.into();
171
172        assert_eq!(expected_result, result);
173    }
174
175    #[test]
176    fn test_torrent_error_from_peer_error() {
177        let err = peer::Error::InvalidPeerId;
178        let expected_result = TorrentError::Peer(peer::Error::InvalidPeerId);
179
180        let result: TorrentError = err.into();
181
182        assert_eq!(expected_result, result);
183    }
184
185    #[test]
186    fn test_torrent_error_from_io_error() {
187        let error = "foo bar";
188        let io_err = io::Error::new(io::ErrorKind::Other, error);
189
190        let result: TorrentError = io_err.into();
191
192        let err_text = result.to_string();
193        assert_eq!(format!("an io error occurred, {}", error), err_text);
194    }
195}