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
pub use std::error::Error as StdError;
use std::fmt;
pub use self::Error::{BlockError, ConfigurationError, InternalError};
pub type Result<T> = ::std::result::Result<T, Error>;
pub trait ResultExtBlock<T, E> {
fn block_error(self, block: &str, message: &str) -> Result<T>;
}
pub trait ResultExtInternal<T, E> {
fn configuration_error(self, message: &str) -> Result<T>;
fn internal_error(self, context: &str, message: &str) -> Result<T>;
}
impl<T, E> ResultExtBlock<T, E> for ::std::result::Result<T, E> {
fn block_error(self, block: &str, message: &str) -> Result<T> {
self.map_err(|_| BlockError(block.to_owned(), message.to_owned()))
}
}
impl<T, E> ResultExtInternal<T, E> for ::std::result::Result<T, E>
where
E: fmt::Display + fmt::Debug,
{
fn configuration_error(self, message: &str) -> Result<T> {
self.map_err(|e| ConfigurationError(message.to_owned(), format!("{}", e)))
}
fn internal_error(self, context: &str, message: &str) -> Result<T> {
self.map_err(|e| {
InternalError(
context.to_owned(),
message.to_owned(),
Some((format!("{}", e), format!("{:?}", e))),
)
})
}
}
pub trait OptionExt<T> {
fn block_error(self, block: &str, message: &str) -> Result<T>;
fn internal_error(self, context: &str, message: &str) -> Result<T>;
}
impl<T> OptionExt<T> for ::std::option::Option<T> {
fn block_error(self, block: &str, message: &str) -> Result<T> {
self.ok_or_else(|| BlockError(block.to_owned(), message.to_owned()))
}
fn internal_error(self, context: &str, message: &str) -> Result<T> {
self.ok_or_else(|| InternalError(context.to_owned(), message.to_owned(), None))
}
}
pub enum Error {
BlockError(String, String),
ConfigurationError(String, String),
InternalError(String, String, Option<(String, String)>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BlockError(ref block, ref message) => {
f.write_str(&format!("Error in block '{}': {}", block, message))
}
ConfigurationError(ref message, _) => {
f.write_str(&format!("Configuration error: {}", message))
}
InternalError(ref context, ref message, _) => f.write_str(&format!(
"Internal error in context '{}': {}",
context, message
)),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BlockError(ref block, ref message) => {
f.write_str(&format!("Error in block '{}': {}", block, message))
}
ConfigurationError(ref message, ref cause) => f.write_str(&format!(
"Configuration error: {}.\nCause: {}",
message, cause
)),
InternalError(ref context, ref message, Some((ref cause, _))) => f.write_str(&format!(
"Internal error in context '{}': {}.\nCause: {}",
context, message, cause
)),
InternalError(ref context, ref message, None) => f.write_str(&format!(
"Internal error in context '{}': {}",
context, message
)),
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
BlockError(_, _) => "Block error occurred in block '{}'",
ConfigurationError(_, _) => "Configuration error occurred",
InternalError(_, _, _) => "Internal error occurred",
}
}
fn cause(&self) -> Option<&dyn StdError> {
None
}
}
impl<T> From<::crossbeam_channel::SendError<T>> for Error
where
T: Send,
{
fn from(_err: ::crossbeam_channel::SendError<T>) -> Error {
InternalError("unknown".to_string(), "send error".to_string(), None)
}
}