socket_server_mocker/
errors.rs

1//! # `server_mocker_error`
2//!
3//! `server_mocker_error` is a type representing an error raised by a server mocker. It's mainly errors raised by the underlying socket server.
4//!
5//! The error is raised directly during call to [`ServerMocker`](crate::ServerMocker) methods, or when the server mocker is running asynchronously and an error occurs.
6//!
7//! If so, errors can be retrieved with [`ServerMocker::pop_server_error`](crate::ServerMocker::pop_server_error) method.
8
9use std::io;
10use std::net::SocketAddr;
11use std::sync::mpsc::SendError;
12
13use crate::Instruction;
14
15/// Represents an error raised by a server mocker.
16///
17/// The error is raised directly during call to [`ServerMocker`](crate::ServerMocker) methods, or when the server mocker is running asynchronously and an error occurs.
18/// If so, errors can be retrieved with [`ServerMocker::pop_server_error`](crate::ServerMocker::pop_server_error) method.
19#[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    /// Indicate if this is a fatal error
46    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}