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
use amqprs::error::Error as AmqprsError;
use std::error::Error as StdError;
use std::fmt::{self, Display};
use tokio::sync::oneshot::error::RecvError;
use tokio::time::error::Elapsed;

#[derive(Debug)]
pub enum AppErrorType {
    InternalError,
    RpcTimeout,
}

#[derive(Debug)]
pub struct AppError {
    pub message: Option<String>,
    pub description: Option<String>,
    pub error_type: AppErrorType,
}
impl Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, r"\{{ {:?}, {:?})\}}", self.message, self.description)
    }
}
impl AppError {
    pub fn new(
        message: Option<String>,
        description: Option<String>,
        error_type: AppErrorType,
    ) -> AppError {
        AppError {
            message,
            description,
            error_type,
        }
    }
    pub fn get_message(&self) -> String {
        match self {
            AppError {
                error_type: AppErrorType::RpcTimeout,
                ..
            } => "The timeout for rpc call was reached".to_string(),
            AppError {
                error_type: AppErrorType::InternalError,
                ..
            } => "An unexpected error has occurred".to_string(),
        }
    }
}

impl From<Box<dyn StdError>> for AppError {
    fn from(error: Box<dyn StdError>) -> AppError {
        println!("{:?}", error);
        AppError {
            message: None,
            description: Some(error.to_string()),
            error_type: AppErrorType::InternalError,
        }
    }
}

impl From<AmqprsError> for AppError {
    fn from(value: AmqprsError) -> Self {
        AppError {
            message: None,
            description: Some(value.to_string()),
            error_type: AppErrorType::InternalError,
        }
    }
}

impl StdError for AppError {
    // The `source` method is optional. If your error type doesn't wrap another error,
    // you can simply return `None`.
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        None
    }
}

impl From<RecvError> for AppError {
    fn from(error: RecvError) -> Self {
        AppError {
            message: None,
            description: Some(error.to_string()),
            error_type: AppErrorType::InternalError,
        }
    }
}
impl From<Elapsed> for AppError {
    fn from(error: Elapsed) -> Self {
        AppError {
            message: None,
            description: Some(error.to_string()),
            error_type: AppErrorType::RpcTimeout,
        }
    }
}