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
use std::{error, fmt::Display};

use downcast_rs::{impl_downcast, Downcast};

pub type AnyError = dyn error::Error + Send + Sync;

#[derive(Debug)]
pub enum BaseError {
    EventNotFound,
    CommandNotFound,
    StopSentinel,
    DatabaseConnectionError(Box<AnyError>),
    TransactionError,
}

impl std::error::Error for BaseError {}
impl Display for BaseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BaseError::CommandNotFound => write!(f, "CommandNotFound"),
            BaseError::EventNotFound => write!(f, "EventNotFound"),
            BaseError::StopSentinel => write!(f, "StopSentinel"),
            BaseError::TransactionError => write!(f, "TransactionError"),
            BaseError::DatabaseConnectionError(res) => write!(f, "{}", res),
        }
    }
}

pub trait ApplicationResponse: 'static {}

pub trait ApplicationError: std::error::Error + Downcast + 'static {}
impl ApplicationError for BaseError {}

impl From<BaseError> for Box<dyn ApplicationError> {
    fn from(value: BaseError) -> Self {
        Box::new(value)
    }
}

impl_downcast!(ApplicationError);

#[macro_export]
macro_rules! ApplicationError {
    (
        $( #[$attr:meta] )*
        $pub:vis
        enum $error_enum:ident {
            $(#[$field_attr:meta])*
            $($variant:ident$($value:ty)?),*$(,)?
        }
    ) => {
        impl std::error::Error for $error_enum {}
        impl $crate::responses::ApplicationError for $error_enum {}
        impl From<$crate::responses::BaseError> for $error_enum {
            fn from(value: $crate::responses::BaseError) -> Self {
                $error_enum::BaseError(value)
            }
        }
        impl From<std::boxed::Box<$error_enum>>
            for std::boxed::Box<dyn $crate::responses::ApplicationError>
        {
            fn from(value: std::boxed::Box<$error_enum>) -> Self {
                value
            }
        }
        impl From<$error_enum> for std::boxed::Box<dyn $crate::responses::ApplicationError> {
            fn from(value: $error_enum) -> Self {
                std::boxed::Box::new(value)
            }
        }
    };
}