1 use std::string::FromUtf8Error; 2 use std::time::SystemTimeError; 3 use std::{fmt, io, num}; 4 5 use tokio::sync::mpsc::error::SendError; 6 7 use url::ParseError; 8 9 #[derive(Debug, Clone, PartialEq)] 10 pub struct Error { 11 message: String, 12 } 13 14 impl fmt::Display for Error { 15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { 16 write!(f, "{}", self.message) 17 } 18 } 19 20 impl std::error::Error for Error { 21 fn description(&self) -> &str { 22 &self.message 23 } 24 } 25 26 // Implement std::convert::From for AppError; from io::Error 27 impl From<io::Error> for Error { 28 fn from(error: io::Error) -> Self { 29 Error { 30 message: error.to_string(), 31 } 32 } 33 } 34 35 impl From<num::ParseIntError> for Error { 36 fn from(error: num::ParseIntError) -> Self { 37 Error { 38 message: error.to_string(), 39 } 40 } 41 } 42 43 impl From<ParseError> for Error { 44 fn from(error: ParseError) -> Self { 45 Error { 46 message: error.to_string(), 47 } 48 } 49 } 50 51 impl From<FromUtf8Error> for Error { 52 fn from(error: FromUtf8Error) -> Self { 53 Error { 54 message: error.to_string(), 55 } 56 } 57 } 58 59 impl From<SystemTimeError> for Error { 60 fn from(error: SystemTimeError) -> Self { 61 Error { 62 message: error.to_string(), 63 } 64 } 65 } 66 67 impl From<SendError> for Error { 68 fn from(error: SendError) -> Self { 69 Error { 70 message: error.to_string(), 71 } 72 } 73 } 74 75 impl Error { 76 pub fn new(message: String) -> Self { 77 Error { message } 78 } 79 } 80