1use core::fmt;
2
3#[derive(Debug, Clone)]
4pub struct Error {
5 status: u16,
6 details: String,
7}
8
9impl Error {
10 pub fn new(status: u16, details: String) -> Self {
11 Self { status, details }
12 }
13
14 pub fn status(&self) -> u16 {
15 self.status
16 }
17
18 pub fn details(&self) -> &str {
19 &self.details
20 }
21}
22
23impl fmt::Display for Error {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25 write!(f, "{}", self.details)
26 }
27}
28
29impl std::error::Error for Error {
30 fn description(&self) -> &str {
31 &self.details
32 }
33}
34
35impl From<oauth2::url::ParseError> for Error {
36 fn from(error: oauth2::url::ParseError) -> Self {
37 Self::new(500, error.to_string())
38 }
39}
40
41impl From<jsonwebtoken::errors::Error> for Error {
42 fn from(error: jsonwebtoken::errors::Error) -> Self {
43 Self::new(500, error.to_string())
44 }
45}
46
47impl From<reqwest::Error> for Error {
48 fn from(error: reqwest::Error) -> Self {
49 let status = error
50 .status()
51 .unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
52 Self::new(status.as_u16(), error.to_string())
53 }
54}