shoal_core/server/
errors.rs

1//! Any errors tht can be encountered when running Shoal
2
3use glommio::GlommioError;
4use glommio::{BuilderErrorKind, ExecutorErrorKind, ReactorErrorKind};
5use std::os::fd::RawFd;
6use std::path::PathBuf;
7use std::time::Duration;
8
9/// Any errors tht can be encountered when running Shoal
10#[derive(Debug)]
11pub enum ServerError {
12    /// An error specific to Shoal code
13    Shoal(ShoalError),
14    /// An IO error
15    IO(std::io::Error),
16    /// A glommio enhanced IO errors
17    GlommioIO {
18        source: std::io::Error,
19        op: &'static str,
20        path: Option<PathBuf>,
21        fd: Option<RawFd>,
22    },
23    /// A glommio executor error
24    GlommioExectorError(ExecutorErrorKind),
25    /// A glommio builder error
26    GlommioBuilderError(BuilderErrorKind),
27    /// A glommio reactor error
28    GlommioReactorError(ReactorErrorKind),
29    /// Glommio time out error
30    GlommioTimedOut(Duration),
31    /// An error from glommio with a generic
32    GlommioGeneric(String),
33    /// An config parsing error
34    Config(config::ConfigError),
35    /// An rkyv error
36    Rkyv(rkyv::rancor::Error),
37    /// An error casting a vec of bytes to a slice
38    IntoSlice(std::array::TryFromSliceError),
39    /// A conversion error
40    Conversion(std::convert::Infallible),
41    /// An error sending a message over a kanal channel
42    KanalSend(kanal::SendError),
43    /// An error receiving a message over a kanal channel
44    KanalRecv(kanal::ReceiveError),
45}
46
47// convert all of our external error types to our error type
48impl<T> From<glommio::GlommioError<T>> for ServerError {
49    fn from(ext: glommio::GlommioError<T>) -> Self {
50        match ext {
51            GlommioError::IoError(error) => Self::IO(error),
52            GlommioError::EnhancedIoError {
53                source,
54                op,
55                path,
56                fd,
57            } => Self::GlommioIO {
58                source,
59                op,
60                path,
61                fd,
62            },
63            GlommioError::ExecutorError(error) => Self::GlommioExectorError(error),
64            GlommioError::BuilderError(error) => Self::GlommioBuilderError(error),
65            GlommioError::ReactorError(error) => Self::GlommioReactorError(error),
66            GlommioError::TimedOut(duration) => Self::GlommioTimedOut(duration),
67            GlommioError::Closed(_) => Self::GlommioGeneric(ext.to_string()),
68            GlommioError::CanNotBeClosed(_, _) => Self::GlommioGeneric(ext.to_string()),
69            GlommioError::WouldBlock(_) => Self::GlommioGeneric(ext.to_string()),
70        }
71    }
72}
73
74impl From<config::ConfigError> for ServerError {
75    /// Convert this error to our error type
76    ///
77    /// # Arguments
78    ///
79    /// * `error` - The errot to convert
80    fn from(error: config::ConfigError) -> Self {
81        ServerError::Config(error)
82    }
83}
84
85impl From<std::io::Error> for ServerError {
86    /// Convert this error to our error type
87    ///
88    /// # Arguments
89    ///
90    /// * `error` - The errot to convert
91    fn from(error: std::io::Error) -> Self {
92        ServerError::IO(error)
93    }
94}
95
96impl From<rkyv::rancor::Error> for ServerError {
97    /// Convert this error to our error type
98    ///
99    /// # Arguments
100    ///
101    /// * `error` - The error to convert
102    fn from(error: rkyv::rancor::Error) -> Self {
103        ServerError::Rkyv(error)
104    }
105}
106
107impl From<std::array::TryFromSliceError> for ServerError {
108    /// Convert this error to our error type
109    ///
110    /// # Arguments
111    ///
112    /// * `error` - The errot to convert
113    fn from(error: std::array::TryFromSliceError) -> Self {
114        ServerError::IntoSlice(error)
115    }
116}
117
118impl From<std::convert::Infallible> for ServerError {
119    /// Convert this error to our error type
120    ///
121    /// # Arguments
122    ///
123    /// * `error` - The errot to convert
124    fn from(error: std::convert::Infallible) -> Self {
125        ServerError::Conversion(error)
126    }
127}
128
129impl From<kanal::SendError> for ServerError {
130    /// Conver this error to our error type
131    ///
132    /// # Arguments
133    ///
134    /// * `error` - The error to convert
135    fn from(error: kanal::SendError) -> Self {
136        ServerError::KanalSend(error)
137    }
138}
139
140impl From<kanal::ReceiveError> for ServerError {
141    /// Conver this error to our error type
142    ///
143    /// # Arguments
144    ///
145    /// * `error` - The error to convert
146    fn from(error: kanal::ReceiveError) -> Self {
147        ServerError::KanalRecv(error)
148    }
149}
150
151/// The errors specific to Shoal server code
152#[derive(Debug)]
153pub enum ShoalError {
154    /// An invalid non binary message type was recieved
155    NonBinaryMessage,
156}