error_mapper/macros/
general.rs

1/// Macro that receives the **error to map**, and uses the From trait to convert it into a standard
2/// **TheError** struct.
3///
4/// The original error message sent from the **origin crate** is mapped into the
5/// self.error.error_content member.
6#[macro_export]
7macro_rules! map_to_new_error {
8    ($error:expr) => {
9
10        $crate::TheError {
11            error: $crate::TheErrorType::from($error),
12            file: Some(file!().to_string()),
13            location: Some((line!(), column!())),
14            datestamp: Some(chrono::Local::now().date_naive()),
15            timestamp: Some(chrono::Local::now().time())
16        }
17    }
18}
19
20#[macro_export]
21macro_rules! create_new_error {
22    ($error_type:expr, $error_content:expr) => {
23        $crate::TheError {
24            error: $crate::TheErrorType {
25                error_type: $error_type,
26                error_content: $error_content.to_string()
27            },
28            file: Some(file!().to_string()),
29            location: Some((line!(), column!())),
30            datestamp: None,
31            timestamp: None
32        }
33    };
34    ($error_content:expr) => {
35        $crate::TheError {
36            error: $crate::TheErrorType {
37                error_type: $crate::SystemErrorCodes::GenericError,
38                error_content: $error_content.to_string()
39            },
40            file: Some(file!().to_string()),
41            location: Some((line!(), column!())),
42            datestamp: None,
43            timestamp: None
44        }
45    }
46}
47
48#[macro_export]
49macro_rules! traceback {
50    ($the_error: expr, $traceback_msg: expr) => {
51        $crate::create_new_error!(
52            format!(
53                "{}; Originated in: {}",
54                $traceback_msg,
55                $the_error.to_string()
56            )
57        );
58    };
59    ($the_error: expr, $traceback_msg: expr, $error_type: expr) => {
60        $crate::create_new_error!(
61        $error_type,
62            format!(
63                "{}; Originated in: {}",
64                $traceback_msg,
65                $the_error.to_string()
66            )
67        );
68    };
69}