forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
100 lines (86 loc) · 3.23 KB
/
error.rs
File metadata and controls
100 lines (86 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Error types for the UDP server.
use std::fmt::Display;
use std::panic::Location;
use aquatic_udp_protocol::{ConnectionId, RequestParseError, TransactionId};
use bittorrent_udp_tracker_core::services::announce::UdpAnnounceError;
use bittorrent_udp_tracker_core::services::scrape::UdpScrapeError;
use derive_more::derive::Display;
use thiserror::Error;
#[derive(Display, Debug)]
#[display(":?")]
pub struct ConnectionCookie(pub ConnectionId);
/// Error returned by the UDP server.
#[derive(Error, Debug, Clone)]
pub enum Error {
/// Error returned when the request is invalid.
#[error("error parsing request: {request_parse_error:?}")]
InvalidRequest { request_parse_error: SendableRequestParseError },
/// Error returned when the domain tracker returns an announce error.
#[error("tracker announce error: {source}")]
AnnounceFailed { source: UdpAnnounceError },
/// Error returned when the domain tracker returns an scrape error.
#[error("tracker scrape error: {source}")]
ScrapeFailed { source: UdpScrapeError },
/// Error returned from a third-party library (`aquatic_udp_protocol`).
#[error("internal server error: {message}, {location}")]
Internal {
location: &'static Location<'static>,
message: String,
},
/// Error returned when tracker requires authentication.
#[error("domain tracker requires authentication but is not supported in current UDP implementation. Location: {location}")]
AuthRequired { location: &'static Location<'static> },
}
impl From<RequestParseError> for Error {
fn from(request_parse_error: RequestParseError) -> Self {
Self::InvalidRequest {
request_parse_error: request_parse_error.into(),
}
}
}
impl From<UdpAnnounceError> for Error {
fn from(udp_announce_error: UdpAnnounceError) -> Self {
Self::AnnounceFailed {
source: udp_announce_error,
}
}
}
impl From<UdpScrapeError> for Error {
fn from(udp_scrape_error: UdpScrapeError) -> Self {
Self::ScrapeFailed {
source: udp_scrape_error,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SendableRequestParseError {
pub message: String,
pub opt_connection_id: Option<ConnectionId>,
pub opt_transaction_id: Option<TransactionId>,
}
impl Display for SendableRequestParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"SendableRequestParseError: message: {}, connection_id: {:?}, transaction_id: {:?}",
self.message, self.opt_connection_id, self.opt_transaction_id
)
}
}
impl From<RequestParseError> for SendableRequestParseError {
fn from(request_parse_error: RequestParseError) -> Self {
let (message, opt_connection_id, opt_transaction_id) = match request_parse_error {
RequestParseError::Sendable {
connection_id,
transaction_id,
err,
} => ((*err).to_string(), Some(connection_id), Some(transaction_id)),
RequestParseError::Unsendable { err } => (err.to_string(), None, None),
};
Self {
message,
opt_connection_id,
opt_transaction_id,
}
}
}