Skip to main content

amqp_client_rust/
errors.rs

1use amqprs::error::Error as AmqprsError;
2use std::error::Error as StdError;
3use std::fmt::{self, Display};
4use tokio::sync::oneshot::error::RecvError;
5use tokio::time::error::Elapsed;
6
7#[derive(Debug, Clone, Copy)]
8pub enum AppErrorType {
9    InternalError,
10    RpcTimeout,
11    TimeoutError,
12    UnexpectedResultError,
13    UnsupportedContentType,
14    NackError,
15}
16
17#[derive(Debug, Clone)]
18pub struct AppError {
19    pub message: Option<String>,
20    pub description: Option<String>,
21    pub error_type: AppErrorType,
22}
23impl Display for AppError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, r"\{{ {:?}, {:?})\}}", self.message, self.description)
26    }
27}
28impl AppError {
29    pub fn new(
30        message: Option<String>,
31        description: Option<String>,
32        error_type: AppErrorType,
33    ) -> AppError {
34        AppError {
35            message,
36            description,
37            error_type,
38        }
39    }
40    pub fn get_message(&self) -> String {
41        match self {
42            AppError {
43                error_type: AppErrorType::RpcTimeout,
44                ..
45            } => "The timeout for rpc call was reached".to_string(),
46            AppError {
47                error_type: AppErrorType::TimeoutError,
48                ..
49            } => "Timeout: failed to connect, order rejected...".to_string(),
50            AppError {
51                error_type: AppErrorType::UnexpectedResultError,
52                ..
53            } => "Unexpected result from eventbus operation".to_string(),
54            AppError {
55                error_type: AppErrorType::UnsupportedContentType,
56                ..
57            } => "Unsupported content type".to_string(),
58            AppError {
59                error_type: AppErrorType::NackError,
60                ..
61            } => "The message was negatively acknowledged".to_string(),
62            AppError {
63                error_type: AppErrorType::InternalError,
64                ..
65            } => "An unexpected error has occurred".to_string(),
66        }
67    }
68}
69
70impl From<Box<dyn StdError>> for AppError {
71    fn from(error: Box<dyn StdError>) -> AppError {
72        AppError {
73            message: None,
74            description: Some(error.to_string()),
75            error_type: AppErrorType::InternalError,
76        }
77    }
78}
79
80#[cfg(feature = "lz4_flex")]
81impl From<lz4_flex::block::DecompressError> for AppError {
82    fn from(error: lz4_flex::block::DecompressError) -> AppError {
83        AppError {
84            message: None,
85            description: Some(error.to_string()),
86            error_type: AppErrorType::InternalError,
87        }
88    }
89}
90
91impl From<AmqprsError> for AppError {
92    fn from(value: AmqprsError) -> Self {
93        AppError {
94            message: None,
95            description: Some(value.to_string()),
96            error_type: AppErrorType::InternalError,
97        }
98    }
99}
100
101impl StdError for AppError {
102    fn source(&self) -> Option<&(dyn StdError + 'static)> {
103        None
104    }
105}
106
107impl From<RecvError> for AppError {
108    fn from(error: RecvError) -> Self {
109        AppError {
110            message: None,
111            description: Some(error.to_string()),
112            error_type: AppErrorType::InternalError,
113        }
114    }
115}
116impl From<Elapsed> for AppError {
117    fn from(error: Elapsed) -> Self {
118        AppError {
119            message: None,
120            description: Some(error.to_string()),
121            error_type: AppErrorType::RpcTimeout,
122        }
123    }
124}