Skip to content

Commit 8f3c22a

Browse files
committed
feat: [torrust#1456] expose error kind in the UdpError event
Not exposing the original complex error type becuase: - It's too complex. - It forces all errors to be "Sent", "PartialEq". - It would expose a lot of internals.
1 parent 52b9660 commit 8f3c22a

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

packages/tracker-core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub enum ScrapeError {
8484
///
8585
/// This error is returned when an operation involves a torrent that is not
8686
/// present in the whitelist.
87-
#[derive(thiserror::Error, Debug, Clone)]
87+
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
8888
pub enum WhitelistError {
8989
/// Indicates that the torrent identified by `info_hash` is not whitelisted.
9090
#[error("The torrent: {info_hash}, is not whitelisted, {location}")]

packages/udp-tracker-core/src/connection_cookie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ use zerocopy::AsBytes;
8686
use crate::crypto::keys::CipherArrayBlowfish;
8787

8888
/// Error returned when there was an error with the connection cookie.
89-
#[derive(Error, Debug, Clone)]
89+
#[derive(Error, Debug, Clone, PartialEq)]
9090
pub enum ConnectionCookieError {
9191
#[error("cookie value is not normal: {not_normal_value}")]
9292
ValueNotNormal { not_normal_value: f64 },

packages/udp-tracker-server/src/error.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Error types for the UDP server.
2+
use std::fmt::Display;
23
use std::panic::Location;
34

45
use aquatic_udp_protocol::{ConnectionId, RequestParseError, TransactionId};
@@ -13,7 +14,7 @@ use torrust_tracker_located_error::LocatedError;
1314
pub struct ConnectionCookie(pub ConnectionId);
1415

1516
/// Error returned by the UDP server.
16-
#[derive(Error, Debug)]
17+
#[derive(Error, Debug, Clone)]
1718
pub enum Error {
1819
/// Error returned when the request is invalid.
1920
#[error("error when phrasing request: {request_parse_error:?}")]
@@ -76,6 +77,16 @@ pub struct SendableRequestParseError {
7677
pub opt_transaction_id: Option<TransactionId>,
7778
}
7879

80+
impl Display for SendableRequestParseError {
81+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82+
write!(
83+
f,
84+
"SendableRequestParseError: message: {}, connection_id: {:?}, transaction_id: {:?}",
85+
self.message, self.opt_connection_id, self.opt_transaction_id
86+
)
87+
}
88+
}
89+
7990
impl From<RequestParseError> for SendableRequestParseError {
8091
fn from(request_parse_error: RequestParseError) -> Self {
8192
let (message, opt_connection_id, opt_transaction_id) = match request_parse_error {

packages/udp-tracker-server/src/event.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ use std::fmt;
22
use std::net::SocketAddr;
33
use std::time::Duration;
44

5+
use bittorrent_tracker_core::error::{AnnounceError, ScrapeError};
6+
use bittorrent_udp_tracker_core::services::announce::UdpAnnounceError;
7+
use bittorrent_udp_tracker_core::services::scrape::UdpScrapeError;
58
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
69
use torrust_tracker_metrics::label_name;
710
use torrust_tracker_primitives::service_binding::ServiceBinding;
811

12+
use crate::error::Error;
13+
914
/// A UDP server event.
10-
#[derive(Debug, PartialEq, Eq, Clone)]
15+
#[derive(Debug, Clone, PartialEq)]
1116
pub enum Event {
1217
UdpRequestReceived {
1318
context: ConnectionContext,
@@ -30,6 +35,7 @@ pub enum Event {
3035
UdpError {
3136
context: ConnectionContext,
3237
kind: Option<UdpRequestKind>,
38+
error: ErrorKind,
3339
},
3440
}
3541

@@ -109,6 +115,43 @@ impl From<ConnectionContext> for LabelSet {
109115
}
110116
}
111117

118+
#[derive(Debug, Clone, PartialEq)]
119+
pub enum ErrorKind {
120+
RequestParse(String),
121+
ConnectionCookie(String),
122+
Whitelist(String),
123+
Database(String),
124+
InternalServer(String),
125+
BadRequest(String),
126+
TrackerAuthentication(String),
127+
}
128+
129+
impl From<Error> for ErrorKind {
130+
fn from(error: Error) -> Self {
131+
match error {
132+
Error::RequestParseError { request_parse_error } => Self::RequestParse(request_parse_error.to_string()),
133+
Error::UdpAnnounceError { source } => match source {
134+
UdpAnnounceError::ConnectionCookieError { source } => Self::ConnectionCookie(source.to_string()),
135+
UdpAnnounceError::TrackerCoreAnnounceError { source } => match source {
136+
AnnounceError::Whitelist(whitelist_error) => Self::Whitelist(whitelist_error.to_string()),
137+
AnnounceError::Database(error) => Self::Database(error.to_string()),
138+
},
139+
UdpAnnounceError::TrackerCoreWhitelistError { source } => Self::Whitelist(source.to_string()),
140+
},
141+
Error::UdpScrapeError { source } => match source {
142+
UdpScrapeError::ConnectionCookieError { source } => Self::ConnectionCookie(source.to_string()),
143+
UdpScrapeError::TrackerCoreScrapeError { source } => match source {
144+
ScrapeError::Whitelist(whitelist_error) => Self::Whitelist(whitelist_error.to_string()),
145+
},
146+
UdpScrapeError::TrackerCoreWhitelistError { source } => Self::Whitelist(source.to_string()),
147+
},
148+
Error::InternalServer { location: _, message } => Self::InternalServer(message.to_string()),
149+
Error::BadRequest { source } => Self::BadRequest(source.to_string()),
150+
Error::TrackerAuthenticationRequired { location } => Self::TrackerAuthentication(location.to_string()),
151+
}
152+
}
153+
}
154+
112155
pub mod sender {
113156
use std::sync::Arc;
114157

packages/udp-tracker-server/src/handlers/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn handle_error(
2121
request_id: Uuid,
2222
opt_udp_server_stats_event_sender: &crate::event::sender::Sender,
2323
cookie_valid_range: Range<f64>,
24-
e: &Error,
24+
error: &Error,
2525
transaction_id: Option<TransactionId>,
2626
) -> Response {
2727
tracing::trace!("handle error");
@@ -31,17 +31,17 @@ pub async fn handle_error(
3131
match transaction_id {
3232
Some(transaction_id) => {
3333
let transaction_id = transaction_id.0.to_string();
34-
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %e, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
34+
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
3535
}
3636
None => {
37-
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %e, %client_socket_addr, %server_socket_addr, %request_id, "response error");
37+
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, "response error");
3838
}
3939
}
4040

41-
let e = if let Error::RequestParseError { request_parse_error } = e {
41+
let e = if let Error::RequestParseError { request_parse_error } = error {
4242
(request_parse_error.message.clone(), transaction_id)
4343
} else {
44-
(e.to_string(), transaction_id)
44+
(error.to_string(), transaction_id)
4545
};
4646

4747
if e.1.is_some() {
@@ -52,6 +52,7 @@ pub async fn handle_error(
5252
.send(Event::UdpError {
5353
context: ConnectionContext::new(client_socket_addr, server_service_binding),
5454
kind: req_kind,
55+
error: error.clone().into(),
5556
})
5657
.await;
5758
}

packages/udp-tracker-server/src/statistics/event/handler.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub async fn handle_event(event: Event, stats_repository: &Repository, now: Dura
232232
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
233233
};
234234
}
235-
Event::UdpError { context, kind } => {
235+
Event::UdpError { context, kind, error: _ } => {
236236
// Global fixed metrics
237237
match context.client_socket_addr().ip() {
238238
std::net::IpAddr::V4(_) => {
@@ -271,7 +271,7 @@ mod tests {
271271
use torrust_tracker_clock::clock::Time;
272272
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
273273

274-
use crate::event::{ConnectionContext, Event, UdpRequestKind};
274+
use crate::event::{ConnectionContext, ErrorKind, Event, UdpRequestKind};
275275
use crate::statistics::event::handler::handle_event;
276276
use crate::statistics::repository::Repository;
277277
use crate::CurrentClock;
@@ -518,6 +518,7 @@ mod tests {
518518
.unwrap(),
519519
),
520520
kind: None,
521+
error: ErrorKind::RequestParse("Invalid request format".to_string()),
521522
},
522523
&stats_repository,
523524
CurrentClock::now(),
@@ -650,6 +651,7 @@ mod tests {
650651
.unwrap(),
651652
),
652653
kind: None,
654+
error: ErrorKind::RequestParse("Invalid request format".to_string()),
653655
},
654656
&stats_repository,
655657
CurrentClock::now(),

0 commit comments

Comments
 (0)