1#[macro_export]
12macro_rules! baxe_error {
13 ( $error_tag_ty:ty, $($extra_attr:meta),* ) => {
14 #[derive(std::fmt::Debug, serde::Serialize)]
15 $(#[$extra_attr])*
16
17 pub struct BaxeError {
18 #[serde(skip)]
19 pub status_code: axum::http::StatusCode,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub message: Option<String>,
22 pub code: u16,
23 pub error_tag: $error_tag_ty,
24 }
25
26 impl std::fmt::Display for BaxeError {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 write!(f, "{:?}", self.message)
29 }
30 }
31
32 impl std::error::Error for BaxeError {}
33
34 impl axum::response::IntoResponse for BaxeError {
35 fn into_response(self) -> axum::response::Response {
36 (self.status_code, axum::Json(self)).into_response()
37 }
38 }
39
40 impl BaxeError {
41 pub fn new(status_code: axum::http::StatusCode, message: Option<String>, code: u16, error_tag: String) -> Self {
42 use std::str::FromStr;
43 Self {
44 status_code,
45 message,
46 code,
47 error_tag: <$error_tag_ty>::from_str(&error_tag).unwrap_or_default()
48 }
49 }
50 }
51 };
52}