Skip to content

Commit f485501

Browse files
committed
refactor: [torrust#1456 clean code
1 parent 0108c26 commit f485501

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ pub async fn handle_error(
2222
opt_udp_server_stats_event_sender: &crate::event::sender::Sender,
2323
cookie_valid_range: Range<f64>,
2424
error: &Error,
25-
transaction_id: Option<TransactionId>,
25+
opt_transaction_id: Option<TransactionId>,
2626
) -> Response {
2727
tracing::trace!("handle error");
2828

2929
let server_socket_addr = server_service_binding.bind_address();
3030

31-
match transaction_id {
31+
match opt_transaction_id {
3232
Some(transaction_id) => {
3333
let transaction_id = transaction_id.0.to_string();
3434
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
@@ -38,13 +38,7 @@ pub async fn handle_error(
3838
}
3939
}
4040

41-
let e = if let Error::RequestParseError { request_parse_error } = error {
42-
(request_parse_error.message.clone(), transaction_id)
43-
} else {
44-
(error.to_string(), transaction_id)
45-
};
46-
47-
if e.1.is_some() {
41+
if opt_transaction_id.is_some() {
4842
// code-review: why we trigger an event only if transaction_id is present?
4943

5044
if let Some(udp_server_stats_event_sender) = opt_udp_server_stats_event_sender.as_deref() {
@@ -59,7 +53,7 @@ pub async fn handle_error(
5953
}
6054

6155
Response::from(ErrorResponse {
62-
transaction_id: e.1.unwrap_or(TransactionId(I32::new(0))),
63-
message: e.0.into(),
56+
transaction_id: opt_transaction_id.unwrap_or(TransactionId(I32::new(0))),
57+
message: error.to_string().into(),
6458
})
6559
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ pub(crate) async fn handle_packet(
110110
},
111111
Err(e) => {
112112
// The request payload could not be parsed, so we handle it as an error.
113+
114+
let opt_transaction_id = if let Error::RequestParseError { request_parse_error } = e.clone() {
115+
request_parse_error.opt_transaction_id
116+
} else {
117+
None
118+
};
119+
113120
let response = handle_error(
114121
None,
115122
udp_request.from,
@@ -118,7 +125,7 @@ pub(crate) async fn handle_packet(
118125
&udp_tracker_server_container.stats_event_sender,
119126
cookie_time_values.valid_range.clone(),
120127
&e,
121-
None,
128+
opt_transaction_id,
122129
)
123130
.await;
124131

0 commit comments

Comments
 (0)