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