1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use axum::{
    extract::rejection::{ExtensionRejection, QueryRejection},
    http::StatusCode,
    response::IntoResponse,
    Json,
};
use serde::{Deserialize, Serialize};

pub type AppResult<T> = Result<T, AppError>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppError {
    #[serde(with = "serde_status_code")]
    status: StatusCode,
    detail: Option<String>,
}

impl Default for AppError {
    fn default() -> Self {
        Self {
            status: StatusCode::INTERNAL_SERVER_ERROR,
            detail: None,
        }
    }
}

impl AppError {
    pub fn with_status(status: StatusCode) -> AppError {
        Self {
            status,
            detail: None,
        }
    }

    /// Create a new [`AppError`].
    pub fn new(status_code: StatusCode, message: Option<impl ToString>) -> AppError {
        Self {
            status: status_code,
            // title: Self::canonical_reason_to_string(&status_code),
            detail: message.map(|m| m.to_string()),
        }
    }
}

impl IntoResponse for AppError {
    fn into_response(self) -> axum::response::Response {
        let json = Json(self.clone());
        (self.status, json).into_response()
    }
}

impl From<anyhow::Error> for AppError {
    fn from(value: anyhow::Error) -> Self {
        Self {
            status: StatusCode::INTERNAL_SERVER_ERROR,
            detail: Some(value.to_string()),
        }
    }
}

impl From<QueryRejection> for AppError {
    fn from(value: QueryRejection) -> Self {
        Self::new(StatusCode::BAD_REQUEST, Some(value))
    }
}

impl From<ExtensionRejection> for AppError {
    fn from(value: ExtensionRejection) -> Self {
        Self::new(StatusCode::BAD_REQUEST, Some(value))
    }
}

/// Serialize/Deserializer for status codes.
///
/// This is needed because status code according to JSON API spec must
/// be the status code as a STRING.
///
/// We could have used http_serde, but it encodes the status code as a NUMBER.
pub mod serde_status_code {
    use http::StatusCode;
    use serde::{de::Unexpected, Deserialize, Deserializer, Serialize, Serializer};

    /// Serialize [StatusCode]s.
    pub fn serialize<S: Serializer>(status: &StatusCode, ser: S) -> Result<S::Ok, S::Error> {
        String::serialize(&status.as_u16().to_string(), ser)
    }

    /// Deserialize [StatusCode]s.
    pub fn deserialize<'de, D>(de: D) -> Result<StatusCode, D::Error>
    where
        D: Deserializer<'de>,
    {
        let str = String::deserialize(de)?;
        StatusCode::from_bytes(str.as_bytes()).map_err(|_| {
            serde::de::Error::invalid_value(
                Unexpected::Str(str.as_str()),
                &"A valid http status code",
            )
        })
    }
}