socket_server_mocker/
errors.rs1use std::io;
10use std::net::SocketAddr;
11use std::sync::mpsc::SendError;
12
13use crate::Instruction;
14
15#[derive(Debug, thiserror::Error)]
20#[allow(missing_docs)]
21pub enum ServerMockerError {
22 #[error("{}: Failed to bind TCP listener to {0}: {1}", self.fatal_str())]
23 UnableToBindListener(SocketAddr, io::Error),
24 #[error("{}: Failed to get local address of a listener: {0}", self.fatal_str())]
25 UnableToGetLocalAddress(io::Error),
26 #[error("{}: Failed to accept incoming connection on {0}: {1}", self.fatal_str())]
27 UnableToAcceptConnection(SocketAddr, io::Error),
28 #[error("{}: Failed to send instructions list to TCP server mocker: {0}", self.fatal_str())]
29 UnableToSendInstructions(SendError<Vec<Instruction>>),
30 #[error("{}: Failed to set read timeout on TCP stream: {0}", self.fatal_str())]
31 UnableToSetReadTimeout(io::Error),
32 #[error("{}: Failed to read from TCP stream: {0}", self.fatal_str())]
33 UnableToReadTcpStream(io::Error),
34 #[error("{}: Failed to write to TCP stream: {0}", self.fatal_str())]
35 UnableToWriteTcpStream(io::Error),
36 #[error("{}: Failed to receive message from client: {0}", self.fatal_str())]
37 UnableToReadUdpStream(io::Error),
38 #[error("{}: SendMessage instruction received before a ReceiveMessage", self.fatal_str())]
39 GotSendMessageBeforeReceiveMessage,
40 #[error("{}: Failed to send message to client: {0}", self.fatal_str())]
41 FailedToSendUdpMessage(io::Error),
42}
43
44impl ServerMockerError {
45 pub fn is_fatal(&self) -> bool {
47 match self {
48 ServerMockerError::UnableToBindListener(_, _)
49 | ServerMockerError::UnableToGetLocalAddress(_)
50 | ServerMockerError::UnableToAcceptConnection(_, _)
51 | ServerMockerError::UnableToSetReadTimeout(_) => true,
52
53 ServerMockerError::UnableToSendInstructions(_)
54 | ServerMockerError::UnableToReadTcpStream(_)
55 | ServerMockerError::UnableToWriteTcpStream(_)
56 | ServerMockerError::UnableToReadUdpStream(_)
57 | ServerMockerError::GotSendMessageBeforeReceiveMessage
58 | ServerMockerError::FailedToSendUdpMessage(_) => false,
59 }
60 }
61
62 fn fatal_str(&self) -> &'static str {
63 if self.is_fatal() {
64 "Fatal"
65 } else {
66 "Non fatal"
67 }
68 }
69}