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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::ExitCode;
use encoding::error::Error as CborError;
use encoding::Error as EncodingError;
use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq)]
#[error("ActorError(fatal: {fatal}, exit_code: {exit_code:?}, msg: {msg})")]
pub struct ActorError {
fatal: bool,
exit_code: ExitCode,
msg: String,
}
impl ActorError {
pub fn new(exit_code: ExitCode, msg: String) -> Self {
Self {
fatal: false,
exit_code,
msg,
}
}
pub fn new_fatal(msg: String) -> Self {
Self {
fatal: true,
exit_code: ExitCode::ErrPlaceholder,
msg,
}
}
pub fn is_fatal(&self) -> bool {
self.fatal
}
pub fn exit_code(&self) -> ExitCode {
self.exit_code
}
pub fn is_ok(&self) -> bool {
self.exit_code == ExitCode::Ok
}
pub fn msg(&self) -> &str {
&self.msg
}
pub fn wrap(mut self, msg: impl AsRef<str>) -> Self {
self.msg = format!("{}: {}", msg.as_ref(), self.msg);
self
}
}
impl From<EncodingError> for ActorError {
fn from(e: EncodingError) -> Self {
Self {
fatal: false,
exit_code: ExitCode::ErrSerialization,
msg: e.to_string(),
}
}
}
impl From<CborError> for ActorError {
fn from(e: CborError) -> Self {
Self {
fatal: false,
exit_code: ExitCode::ErrSerialization,
msg: e.to_string(),
}
}
}
#[macro_export]
macro_rules! actor_error {
( fatal($msg:expr) ) => { ActorError::new_fatal($msg.to_string()) };
( fatal($msg:literal $(, $ex:expr)+) ) => {
ActorError::new_fatal(format!($msg, $($ex,)*))
};
( $code:ident; $msg:expr ) => { ActorError::new(ExitCode::$code, $msg.to_string()) };
( $code:ident; $msg:literal $(, $ex:expr)+ ) => {
ActorError::new(ExitCode::$code, format!($msg, $($ex,)*))
};
( $code:ident, $msg:expr ) => { actor_error!($code; $msg) };
( $code:ident, $msg:literal $(, $ex:expr)+ ) => {
actor_error!($code; $msg $(, $ex)*)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_macro_generation() {
assert_eq!(
actor_error!(SysErrSenderInvalid; "test"),
ActorError::new(ExitCode::SysErrSenderInvalid, "test".to_owned())
);
assert_eq!(
actor_error!(SysErrSenderInvalid; "test {}, {}", 8, 10),
ActorError::new(ExitCode::SysErrSenderInvalid, format!("test {}, {}", 8, 10))
);
assert_eq!(
actor_error!(fatal("test {}, {}", 8, 10)),
ActorError::new_fatal(format!("test {}, {}", 8, 10))
);
}
}