shuttle_common/models/
error.rs

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::fmt::{Display, Formatter};

use http::StatusCode;
use serde::{Deserialize, Serialize};

#[cfg(feature = "display")]
use crossterm::style::Stylize;

#[cfg(feature = "axum")]
impl axum::response::IntoResponse for ApiError {
    fn into_response(self) -> axum::response::Response {
        tracing::warn!("{}", self.message);

        (self.status(), axum::Json(self)).into_response()
    }
}

#[derive(Serialize, Deserialize, Debug)]
#[typeshare::typeshare]
pub struct ApiError {
    pub message: String,
    pub status_code: u16,
}

impl ApiError {
    pub fn internal(message: &str) -> Self {
        Self {
            message: message.to_string(),
            status_code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
        }
    }

    /// Creates an internal error without exposing sensitive information to the user.
    #[inline(always)]
    #[allow(unused_variables)]
    pub fn internal_safe<E>(message: &str, error: E) -> Self
    where
        E: std::error::Error + 'static,
    {
        tracing::error!(error = &error as &dyn std::error::Error, "{message}");

        // Return the raw error during debug builds
        #[cfg(debug_assertions)]
        {
            ApiError::internal(&error.to_string())
        }
        // Return the safe message during release builds
        #[cfg(not(debug_assertions))]
        {
            ApiError::internal(message)
        }
    }

    pub fn unavailable(error: impl std::error::Error) -> Self {
        Self {
            message: error.to_string(),
            status_code: StatusCode::SERVICE_UNAVAILABLE.as_u16(),
        }
    }

    pub fn bad_request(error: impl std::error::Error) -> Self {
        Self {
            message: error.to_string(),
            status_code: StatusCode::BAD_REQUEST.as_u16(),
        }
    }

    pub fn unauthorized() -> Self {
        Self {
            message: "Unauthorized".to_string(),
            status_code: StatusCode::UNAUTHORIZED.as_u16(),
        }
    }

    pub fn forbidden() -> Self {
        Self {
            message: "Forbidden".to_string(),
            status_code: StatusCode::FORBIDDEN.as_u16(),
        }
    }

    pub fn status(&self) -> StatusCode {
        StatusCode::from_u16(self.status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
    }
}

pub trait ErrorContext<T> {
    /// Make a new internal server error with the given message.
    #[inline(always)]
    fn context_internal_error(self, message: &str) -> Result<T, ApiError>
    where
        Self: Sized,
    {
        self.with_context_internal_error(move || message.to_string())
    }

    /// Make a new internal server error using the given function to create the message.
    fn with_context_internal_error(self, message: impl FnOnce() -> String) -> Result<T, ApiError>;

    /// Make a new bad request error with the given message.
    #[inline(always)]
    fn context_bad_request(self, message: &str) -> Result<T, ApiError>
    where
        Self: Sized,
    {
        self.with_context_bad_request(move || message.to_string())
    }

    /// Make a new bad request error using the given function to create the message.
    fn with_context_bad_request(self, message: impl FnOnce() -> String) -> Result<T, ApiError>;

    /// Make a new not found error with the given message.
    #[inline(always)]
    fn context_not_found(self, message: &str) -> Result<T, ApiError>
    where
        Self: Sized,
    {
        self.with_context_not_found(move || message.to_string())
    }

    /// Make a new not found error using the given function to create the message.
    fn with_context_not_found(self, message: impl FnOnce() -> String) -> Result<T, ApiError>;
}

impl<T, E> ErrorContext<T> for Result<T, E>
where
    E: std::error::Error + 'static,
{
    #[inline(always)]
    fn with_context_internal_error(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Ok(value) => Ok(value),
            Err(error) => Err(ApiError::internal_safe(message().as_ref(), error)),
        }
    }

    #[inline(always)]
    fn with_context_bad_request(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Ok(value) => Ok(value),
            Err(error) => Err({
                let message = message();
                tracing::warn!(
                    error = &error as &dyn std::error::Error,
                    "bad request: {message}"
                );

                ApiError {
                    message,
                    status_code: StatusCode::BAD_REQUEST.as_u16(),
                }
            }),
        }
    }

    #[inline(always)]
    fn with_context_not_found(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Ok(value) => Ok(value),
            Err(error) => Err({
                let message = message();
                tracing::warn!(
                    error = &error as &dyn std::error::Error,
                    "not found: {message}"
                );

                ApiError {
                    message,
                    status_code: StatusCode::NOT_FOUND.as_u16(),
                }
            }),
        }
    }
}

impl<T> ErrorContext<T> for Option<T> {
    #[inline]
    fn with_context_internal_error(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Some(value) => Ok(value),
            None => Err(ApiError::internal(message().as_ref())),
        }
    }

    #[inline]
    fn with_context_bad_request(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Some(value) => Ok(value),
            None => Err({
                ApiError {
                    message: message(),
                    status_code: StatusCode::BAD_REQUEST.as_u16(),
                }
            }),
        }
    }

    #[inline]
    fn with_context_not_found(self, message: impl FnOnce() -> String) -> Result<T, ApiError> {
        match self {
            Some(value) => Ok(value),
            None => Err({
                ApiError {
                    message: message(),
                    status_code: StatusCode::NOT_FOUND.as_u16(),
                }
            }),
        }
    }
}

impl Display for ApiError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        #[cfg(feature = "display")]
        return write!(
            f,
            "{}\nMessage: {}",
            self.status().to_string().bold(),
            self.message.to_string().red()
        );
        #[cfg(not(feature = "display"))]
        return write!(f, "{}\nMessage: {}", self.status(), self.message);
    }
}

impl std::error::Error for ApiError {}