1use std::{
2 borrow::Cow,
3 fmt::Display,
4 sync::{MutexGuard, PoisonError, RwLockReadGuard, RwLockWriteGuard},
5};
6
7pub type Result<T, E = Error> = std::result::Result<T, E>;
9pub type AnyError = Box<dyn std::error::Error>;
11pub type AnyResult<T> = std::result::Result<T, AnyError>;
13
14#[derive(thiserror::Error, Debug, Clone)]
16pub enum Error {
17 #[error("Decoding error: {0}")]
19 Decode(String),
20 #[error("RwLock read error: {0}")]
22 RwLockReadError(String),
23 #[error("RwLock write error: {0}")]
25 RwLockWriteError(String),
26 #[error("Mutex error: {0}")]
28 MutexError(String),
29 #[error("Mpsc Recv Error: {0}")]
31 RecvError(#[from] std::sync::mpsc::RecvError),
32 #[error("400 Bad Request")]
34 BadRequest,
35 #[error("Exists: {0}")]
37 Exists(Cow<'static, str>),
38 #[error("Str: {0}")]
44 Str(Cow<'static, str>),
45 #[error("String: {0}")]
47 String(String),
48 #[cfg(feature = "regex")]
50 #[error("regex error: {0}")]
51 Regex(#[from] regex::Error),
52 #[error("authentication required: {0}")]
54 Unauthorized(&'static str),
55 #[error("user may not perform that action")]
57 Forbidden,
58 #[error("request path not found: {0}")]
60 NotFound(Cow<'static, str>),
61 #[error("Option<{0}>")]
63 Option(Cow<'static, str>),
64 #[error("Not supported: {0}")]
66 Unsupport(Cow<'static, str>),
67 #[error("TryFromSlice: {0}")]
69 TryFromSlice(#[from] std::array::TryFromSliceError),
70 #[error("env var error : {0}")]
75 Env(#[from] std::env::VarError),
76 #[error("Empty")]
78 Empty,
79 #[error("an error occurred with the parse Error: {0}")]
84 ParseNumber(#[from] std::num::ParseIntError),
85 #[error("an error occurred with the parse Error: {0}")]
87 ParseFloatError(#[from] std::num::ParseFloatError),
88 #[error("Log: {0}")]
90 Log(Cow<'static, str>),
91 #[error("CString: {0}")]
93 Nul(#[from] std::ffi::NulError),
94 #[error("{0}")]
96 Any(String),
97}
98
99impl From<String> for Error {
100 fn from(s: String) -> Self {
101 Error::String(s)
102 }
103}
104impl From<&'static str> for Error {
105 fn from(s: &'static str) -> Self {
106 Error::Str(Cow::Borrowed(s))
107 }
108}
109impl<T: Send + Sync + 'static> From<PoisonError<RwLockReadGuard<'_, T>>> for Error {
110 fn from(err: PoisonError<RwLockReadGuard<'_, T>>) -> Self {
111 Error::RwLockReadError(err.to_string())
112 }
113}
114
115impl<T: Send + Sync + 'static> From<PoisonError<MutexGuard<'_, T>>> for Error {
116 fn from(err: PoisonError<MutexGuard<'_, T>>) -> Self {
117 Error::MutexError(err.to_string())
118 }
119}
120
121impl<T: Send + Sync + 'static> From<PoisonError<RwLockWriteGuard<'_, T>>> for Error {
122 fn from(err: PoisonError<RwLockWriteGuard<'_, T>>) -> Self {
123 Error::RwLockWriteError(err.to_string())
124 }
125}
126
127pub trait AnyRes<T, E> {
129 fn any(self) -> Result<T>;
131}
132
133impl<T, E: Display> AnyRes<T, E> for std::result::Result<T, E> {
134 fn any(self) -> Result<T> {
135 match self {
136 Ok(v) => Result::Ok(v),
137 Err(e) => Result::Err(Error::Any(e.to_string())),
138 }
139 }
140}