catalyzer/
error.rs

1use core::{fmt, cmp};
2pub(crate) mod inner {
3    pub(crate) use std::io::ErrorKind as IoErrorKind;
4    pub(crate) use std::string::FromUtf8Error;
5    pub(crate) use std::io::Error as IoError;
6    use super::*;
7
8    /// A cloneable, transparent wrapper around an I/O error.
9    #[repr(transparent)]
10    pub struct CatalyzerIoError(pub(super) IoError);
11    impl CatalyzerIoError {
12        /// Returns the kind of the error.
13        #[inline]
14        pub fn kind(&self) -> IoErrorKind {
15            self.0.kind()
16        }
17    }
18    impl Clone for CatalyzerIoError {
19        #[inline]
20        fn clone(&self) -> Self {
21            Self(IoError::new(self.0.kind(), self.0.to_string()))
22        }
23    }
24    impl fmt::Debug for CatalyzerIoError {
25        #[inline]
26        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27            self.0.fmt(f)
28        }
29    }
30    impl fmt::Display for CatalyzerIoError {
31        #[inline]
32        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33            self.0.fmt(f)
34        }
35    }
36    impl cmp::PartialEq for CatalyzerIoError {
37        #[inline]
38        fn eq(&self, other: &Self) -> bool {
39            self.0.kind().eq(&other.0.kind())
40        }
41    }
42    impl cmp::Eq for CatalyzerIoError {}
43    impl From<IoError> for CatalyzerIoError {
44        #[inline]
45        fn from(e: IoError) -> Self {
46            Self(e)
47        }
48    }
49    impl From<CatalyzerIoError> for IoError {
50        #[inline]
51        fn from(e: CatalyzerIoError) -> Self {
52            e.0
53        }
54    }
55    impl std::error::Error for CatalyzerIoError {}
56
57    /// An Inner error type for Catalyzer operations.
58    #[derive(Debug, Clone)]
59    pub enum CatalyzerError {
60        /// An I/O error occurred.
61        Io(CatalyzerIoError),
62        /// An error occurred while converting a byte array to a UTF-8 string.
63        Utf8(FromUtf8Error),
64        /// An error occurred while initializing the runtime.
65        RuntimeInitializationError,
66        /// The provided method is not supported.
67        UnsupportedMethodError,
68        /// No address was provided.
69        NoAddress,
70    }
71    impl std::error::Error for CatalyzerError {}
72    impl fmt::Display for CatalyzerError {
73        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74            match self {
75                Self::Io(e) => write!(f, "An I/O error occurred: {}", e),
76                Self::Utf8(_) => write!(f, "An error occurred while converting a byte array to a UTF-8 string"),
77                Self::RuntimeInitializationError => write!(f, "An error occurred while initializing the runtime"),
78                Self::UnsupportedMethodError => write!(f, "The provided method is not supported"),
79                Self::NoAddress => write!(f, "No address was provided"),
80            }
81        }
82    }
83}
84
85pub(crate) use inner::{CatalyzerError as Inner, *};
86
87/// An error type for Catalyzer operations.
88/// 
89/// This type is a wrapper around various error types that can occur during
90/// Catalyzer operations.
91/// 
92/// For ease of use, you can always use the `?` operator to propagate errors.
93#[repr(transparent)]
94#[derive(Debug, Clone)]
95pub struct CatalyzerError(Inner);
96
97#[allow(non_upper_case_globals)]
98impl CatalyzerError {
99    /// A shortcut for creating a [`RuntimeInitializationError`].
100    /// 
101    /// [`RuntimeInitializationError`]: crate::internals::InnerCatalyzerError::RuntimeInitializationError
102    pub const RuntimeInitializationError: Self = Self(Inner::RuntimeInitializationError);
103    /// A shortcut for creating a [`UnsupportedMethodError`].
104    /// 
105    /// [`UnsupportedMethodError`]: crate::internals::InnerCatalyzerError::UnsupportedMethodError
106    pub const UnsupportedMethodError: Self = Self(Inner::UnsupportedMethodError);
107    /// A shortcut for creating a [`NoAddress`] error.
108    /// 
109    /// [`NoAddress`]: crate::internals::InnerCatalyzerError::NoAddress
110    pub const NoAddress: Self = Self(Inner::NoAddress);
111    /// Creates a new `CatalyzerError` from the given inner error.
112    #[inline]
113    pub const fn new(inner: Inner) -> Self {
114        Self(inner)
115    }
116    /// Returns the inner error.
117    #[inline]
118    pub fn into_inner(self) -> Inner {
119        self.0
120    }
121}
122
123impl std::error::Error for CatalyzerError {}
124
125impl fmt::Display for CatalyzerError {
126    #[inline]
127    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128        self.0.fmt(f)
129    }
130}
131
132impl From<CatalyzerIoError> for CatalyzerError {
133    #[inline]
134    fn from(e: CatalyzerIoError) -> Self {
135        Self(Inner::Io(e))
136    }
137}
138
139impl From<IoError> for CatalyzerError {
140    #[inline]
141    fn from(e: IoError) -> Self {
142        Self(Inner::Io(CatalyzerIoError(e)))
143    }
144}
145
146impl From<FromUtf8Error> for CatalyzerError {
147    #[inline]
148    fn from(e: FromUtf8Error) -> Self {
149        Self(Inner::Utf8(e))
150    }
151}
152
153impl From<Inner> for CatalyzerError {
154    #[inline]
155    fn from(e: Inner) -> Self {
156        Self(e)
157    }
158}
159
160use core::result::Result as R;
161/// A specialized `Result` type for Catalyzer operations.
162pub type Result<T = (), E = CatalyzerError> = R<T, E>;
163
164impl ::axum::response::IntoResponse for CatalyzerError {
165    fn into_response(self) -> axum::response::Response {
166        let v = axum::http::Response::builder()
167            .status(::axum::http::StatusCode::INTERNAL_SERVER_ERROR)
168            .body(self.to_string().into());
169        #[cfg(debug_assertions)]
170        { v.unwrap_or_default() }
171        #[cfg(not(debug_assertions))]
172        unsafe { v.unwrap_unchecked() }
173    }
174}