xref: /webrtc/util/src/error.rs (revision 89fd1a02)
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 From<p256::elliptic_curve::Error> for Error {
95     fn from(error: p256::elliptic_curve::Error) -> Self {
96         Error {
97             message: error.to_string(),
98         }
99     }
100 }
101 
102 impl From<block_modes::InvalidKeyIvLength> for Error {
103     fn from(error: block_modes::InvalidKeyIvLength) -> Self {
104         Error {
105             message: error.to_string(),
106         }
107     }
108 }
109 impl From<block_modes::BlockModeError> for Error {
110     fn from(error: block_modes::BlockModeError) -> Self {
111         Error {
112             message: error.to_string(),
113         }
114     }
115 }
116 
117 impl From<rsa::errors::Error> for Error {
118     fn from(error: rsa::errors::Error) -> Self {
119         Error {
120             message: error.to_string(),
121         }
122     }
123 }
124 
125 impl From<signature::Error> for Error {
126     fn from(error: signature::Error) -> Self {
127         Error {
128             message: error.to_string(),
129         }
130     }
131 }
132 
133 impl Error {
134     pub fn new(message: String) -> Self {
135         Error { message }
136     }
137 }
138