Skip to content

Commit 52b9660

Browse files
committed
feat: [torrust#1456] wrapper over aquatic RequestParseError to make it sendable
The error will be included in the UdpError event ans sent via tokio channel.
1 parent 059d2b6 commit 52b9660

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Error types for the UDP server.
22
use std::panic::Location;
33

4-
use aquatic_udp_protocol::{ConnectionId, RequestParseError};
4+
use aquatic_udp_protocol::{ConnectionId, RequestParseError, TransactionId};
55
use bittorrent_udp_tracker_core::services::announce::UdpAnnounceError;
66
use bittorrent_udp_tracker_core::services::scrape::UdpScrapeError;
77
use derive_more::derive::Display;
@@ -17,7 +17,7 @@ pub struct ConnectionCookie(pub ConnectionId);
1717
pub enum Error {
1818
/// Error returned when the request is invalid.
1919
#[error("error when phrasing request: {request_parse_error:?}")]
20-
RequestParseError { request_parse_error: RequestParseError },
20+
RequestParseError { request_parse_error: SendableRequestParseError },
2121

2222
/// Error returned when the domain tracker returns an announce error.
2323
#[error("tracker announce error: {source}")]
@@ -47,7 +47,9 @@ pub enum Error {
4747

4848
impl From<RequestParseError> for Error {
4949
fn from(request_parse_error: RequestParseError) -> Self {
50-
Self::RequestParseError { request_parse_error }
50+
Self::RequestParseError {
51+
request_parse_error: request_parse_error.into(),
52+
}
5153
}
5254
}
5355

@@ -66,3 +68,29 @@ impl From<UdpScrapeError> for Error {
6668
}
6769
}
6870
}
71+
72+
#[derive(Debug, PartialEq, Eq, Clone)]
73+
pub struct SendableRequestParseError {
74+
pub message: String,
75+
pub opt_connection_id: Option<ConnectionId>,
76+
pub opt_transaction_id: Option<TransactionId>,
77+
}
78+
79+
impl From<RequestParseError> for SendableRequestParseError {
80+
fn from(request_parse_error: RequestParseError) -> Self {
81+
let (message, opt_connection_id, opt_transaction_id) = match request_parse_error {
82+
RequestParseError::Sendable {
83+
connection_id,
84+
transaction_id,
85+
err,
86+
} => ((*err).to_string(), Some(connection_id), Some(transaction_id)),
87+
RequestParseError::Unsendable { err } => (err.to_string(), None, None),
88+
};
89+
90+
Self {
91+
message,
92+
opt_connection_id,
93+
opt_transaction_id,
94+
}
95+
}
96+
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
use std::net::SocketAddr;
33
use std::ops::Range;
44

5-
use aquatic_udp_protocol::{ErrorResponse, RequestParseError, Response, TransactionId};
6-
use bittorrent_udp_tracker_core::connection_cookie::{check, gen_remote_fingerprint};
5+
use aquatic_udp_protocol::{ErrorResponse, Response, TransactionId};
76
use bittorrent_udp_tracker_core::{self, UDP_TRACKER_LOG_TARGET};
87
use torrust_tracker_primitives::service_binding::ServiceBinding;
98
use tracing::{instrument, Level};
@@ -40,25 +39,14 @@ pub async fn handle_error(
4039
}
4140

4241
let e = if let Error::RequestParseError { request_parse_error } = e {
43-
match request_parse_error {
44-
RequestParseError::Sendable {
45-
connection_id,
46-
transaction_id,
47-
err,
48-
} => {
49-
if let Err(e) = check(connection_id, gen_remote_fingerprint(&client_socket_addr), cookie_valid_range) {
50-
(e.to_string(), Some(*transaction_id))
51-
} else {
52-
((*err).to_string(), Some(*transaction_id))
53-
}
54-
}
55-
RequestParseError::Unsendable { err } => (err.to_string(), transaction_id),
56-
}
42+
(request_parse_error.message.clone(), transaction_id)
5743
} else {
5844
(e.to_string(), transaction_id)
5945
};
6046

6147
if e.1.is_some() {
48+
// code-review: why we trigger an event only if transaction_id is present?
49+
6250
if let Some(udp_server_stats_event_sender) = opt_udp_server_stats_event_sender.as_deref() {
6351
udp_server_stats_event_sender
6452
.send(Event::UdpError {

0 commit comments

Comments
 (0)