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
82 lines (73 loc) · 2.74 KB
/
error.rs
File metadata and controls
82 lines (73 loc) · 2.74 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
//! UDP tracker error handling.
use std::net::SocketAddr;
use std::ops::Range;
use aquatic_udp_protocol::{ErrorResponse, Response, TransactionId};
use bittorrent_udp_tracker_core::{self, UDP_TRACKER_LOG_TARGET};
use torrust_tracker_primitives::service_binding::ServiceBinding;
use tracing::{instrument, Level};
use uuid::Uuid;
use zerocopy::network_endian::I32;
use crate::error::Error;
use crate::event::{ConnectionContext, Event, UdpRequestKind};
#[allow(clippy::too_many_arguments)]
#[instrument(fields(transaction_id), skip(opt_udp_server_stats_event_sender), ret(level = Level::TRACE))]
pub async fn handle_error(
req_kind: Option<UdpRequestKind>,
client_socket_addr: SocketAddr,
server_service_binding: ServiceBinding,
request_id: Uuid,
opt_udp_server_stats_event_sender: &crate::event::sender::Sender,
cookie_valid_range: Range<f64>,
error: &Error,
opt_transaction_id: Option<TransactionId>,
) -> Response {
tracing::trace!("handle error");
let server_socket_addr = server_service_binding.bind_address();
log_error(error, client_socket_addr, server_socket_addr, opt_transaction_id, request_id);
trigger_udp_error_event(
error,
client_socket_addr,
server_service_binding,
opt_udp_server_stats_event_sender,
req_kind,
)
.await;
Response::from(ErrorResponse {
transaction_id: opt_transaction_id.unwrap_or(TransactionId(I32::new(0))),
message: error.to_string().into(),
})
}
fn log_error(
error: &Error,
client_socket_addr: SocketAddr,
server_socket_addr: SocketAddr,
opt_transaction_id: Option<TransactionId>,
request_id: Uuid,
) {
match opt_transaction_id {
Some(transaction_id) => {
let transaction_id = transaction_id.0.to_string();
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
}
None => {
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, "response error");
}
}
}
async fn trigger_udp_error_event(
error: &Error,
client_socket_addr: SocketAddr,
server_service_binding: ServiceBinding,
opt_udp_server_stats_event_sender: &crate::event::sender::Sender,
req_kind: Option<UdpRequestKind>,
) {
if let Some(udp_server_stats_event_sender) = opt_udp_server_stats_event_sender.as_deref() {
udp_server_stats_event_sender
.send(Event::UdpError {
context: ConnectionContext::new(client_socket_addr, server_service_binding),
kind: req_kind,
error: error.clone().into(),
})
.await;
}
}