|
1 | 1 | //! The Error type that you can expect when using this library
|
2 | 2 |
|
3 |
| -use std::error::Error; |
4 |
| -use std::fmt; |
| 3 | +use std::{ |
| 4 | + convert::Infallible, |
| 5 | + num::{ParseFloatError, ParseIntError}, |
| 6 | + str::ParseBoolError, |
| 7 | +}; |
| 8 | + |
| 9 | +use base64::DecodeError; |
5 | 10 |
|
6 | 11 | /// The default Error type for all Atomic Lib Errors.
|
7 |
| -// TODO: specify & limit error types |
8 |
| -// https://github.com/joepio/atomic/issues/11 |
9 |
| -pub type AtomicResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; |
| 12 | +pub type AtomicResult<T> = std::result::Result<T, AtomicError>; |
| 13 | + |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct AtomicError { |
| 16 | + pub message: String, |
| 17 | + pub error_type: AtomicErrorType, |
| 18 | +} |
10 | 19 |
|
11 | 20 | #[derive(Debug)]
|
12 |
| -struct AtomicError(String); |
| 21 | +pub enum AtomicErrorType { |
| 22 | + NotFoundError, |
| 23 | + UnauthorizedError, |
| 24 | + OtherError, |
| 25 | +} |
| 26 | + |
| 27 | +impl std::error::Error for AtomicError { |
| 28 | + // fn description(&self) -> &str { |
| 29 | + // // Both underlying errors already impl `Error`, so we defer to their |
| 30 | + // // implementations. |
| 31 | + // match *self { |
| 32 | + // CliError::Io(ref err) => err.description(), |
| 33 | + // // Normally we can just write `err.description()`, but the error |
| 34 | + // // type has a concrete method called `description`, which conflicts |
| 35 | + // // with the trait method. For now, we must explicitly call |
| 36 | + // // `description` through the `Error` trait. |
| 37 | + // CliError::Parse(ref err) => error::Error::description(err), |
| 38 | + // } |
| 39 | + // } |
| 40 | + |
| 41 | + // fn cause(&self) -> Option<&dyn std::error::Error> { |
| 42 | + // match *self { |
| 43 | + // // N.B. Both of these implicitly cast `err` from their concrete |
| 44 | + // // types (either `&io::Error` or `&num::ParseIntError`) |
| 45 | + // // to a trait object `&Error`. This works because both error types |
| 46 | + // // implement `Error`. |
| 47 | + // CliError::Io(ref err) => Some(err), |
| 48 | + // CliError::Parse(ref err) => Some(err), |
| 49 | + // } |
| 50 | + // } |
| 51 | +} |
| 52 | + |
| 53 | +impl AtomicError { |
| 54 | + #[allow(dead_code)] |
| 55 | + pub fn not_found(message: String) -> AtomicError { |
| 56 | + AtomicError { |
| 57 | + message: format!("Resource not found. {}", message), |
| 58 | + error_type: AtomicErrorType::NotFoundError, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + pub fn unauthorized(message: String) -> AtomicError { |
| 63 | + AtomicError { |
| 64 | + message: format!("Unauthorized. {}", message), |
| 65 | + error_type: AtomicErrorType::UnauthorizedError, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub fn other_error(message: String) -> AtomicError { |
| 70 | + AtomicError { |
| 71 | + message, |
| 72 | + error_type: AtomicErrorType::OtherError, |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl std::fmt::Display for AtomicError { |
| 78 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 79 | + write!(f, "{}", &self.message) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Error conversions |
| 84 | +impl From<&str> for AtomicError { |
| 85 | + fn from(message: &str) -> Self { |
| 86 | + AtomicError { |
| 87 | + message: message.into(), |
| 88 | + error_type: AtomicErrorType::OtherError, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl From<String> for AtomicError { |
| 94 | + fn from(message: String) -> Self { |
| 95 | + AtomicError { |
| 96 | + message, |
| 97 | + error_type: AtomicErrorType::OtherError, |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// The following feel very redundant. Can this be simplified? |
| 103 | + |
| 104 | +impl From<std::boxed::Box<dyn std::error::Error>> for AtomicError { |
| 105 | + fn from(error: std::boxed::Box<dyn std::error::Error>) -> Self { |
| 106 | + AtomicError { |
| 107 | + message: error.to_string(), |
| 108 | + error_type: AtomicErrorType::OtherError, |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<T> From<std::sync::PoisonError<T>> for AtomicError { |
| 114 | + fn from(error: std::sync::PoisonError<T>) -> Self { |
| 115 | + AtomicError { |
| 116 | + message: error.to_string(), |
| 117 | + error_type: AtomicErrorType::OtherError, |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl From<std::io::Error> for AtomicError { |
| 123 | + fn from(error: std::io::Error) -> Self { |
| 124 | + AtomicError { |
| 125 | + message: error.to_string(), |
| 126 | + error_type: AtomicErrorType::OtherError, |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl From<url::ParseError> for AtomicError { |
| 132 | + fn from(error: url::ParseError) -> Self { |
| 133 | + AtomicError { |
| 134 | + message: error.to_string(), |
| 135 | + error_type: AtomicErrorType::OtherError, |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl From<serde_json::Error> for AtomicError { |
| 141 | + fn from(error: serde_json::Error) -> Self { |
| 142 | + AtomicError { |
| 143 | + message: error.to_string(), |
| 144 | + error_type: AtomicErrorType::OtherError, |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl From<std::string::FromUtf8Error> for AtomicError { |
| 150 | + fn from(error: std::string::FromUtf8Error) -> Self { |
| 151 | + AtomicError { |
| 152 | + message: error.to_string(), |
| 153 | + error_type: AtomicErrorType::OtherError, |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +impl From<ParseFloatError> for AtomicError { |
| 159 | + fn from(error: ParseFloatError) -> Self { |
| 160 | + AtomicError { |
| 161 | + message: error.to_string(), |
| 162 | + error_type: AtomicErrorType::OtherError, |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl From<ParseIntError> for AtomicError { |
| 168 | + fn from(error: ParseIntError) -> Self { |
| 169 | + AtomicError { |
| 170 | + message: error.to_string(), |
| 171 | + error_type: AtomicErrorType::OtherError, |
| 172 | + } |
| 173 | + } |
| 174 | +} |
13 | 175 |
|
14 |
| -impl fmt::Display for AtomicError { |
15 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
16 |
| - write!(f, "There is an error: {}", self.0) |
| 176 | +impl From<DecodeError> for AtomicError { |
| 177 | + fn from(error: DecodeError) -> Self { |
| 178 | + AtomicError { |
| 179 | + message: error.to_string(), |
| 180 | + error_type: AtomicErrorType::OtherError, |
| 181 | + } |
17 | 182 | }
|
18 | 183 | }
|
19 | 184 |
|
20 |
| -impl Error for AtomicError {} |
| 185 | +impl From<ParseBoolError> for AtomicError { |
| 186 | + fn from(error: ParseBoolError) -> Self { |
| 187 | + AtomicError { |
| 188 | + message: error.to_string(), |
| 189 | + error_type: AtomicErrorType::OtherError, |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +impl From<Infallible> for AtomicError { |
| 195 | + fn from(error: Infallible) -> Self { |
| 196 | + AtomicError { |
| 197 | + message: error.to_string(), |
| 198 | + error_type: AtomicErrorType::OtherError, |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +#[cfg(feature = "db")] |
| 204 | +impl From<sled::Error> for AtomicError { |
| 205 | + fn from(error: sled::Error) -> Self { |
| 206 | + AtomicError { |
| 207 | + message: error.to_string(), |
| 208 | + error_type: AtomicErrorType::OtherError, |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +#[cfg(feature = "db")] |
| 214 | +impl From<Box<bincode::ErrorKind>> for AtomicError { |
| 215 | + fn from(error: Box<bincode::ErrorKind>) -> Self { |
| 216 | + AtomicError { |
| 217 | + message: error.to_string(), |
| 218 | + error_type: AtomicErrorType::OtherError, |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments