-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy patherror.rs
More file actions
114 lines (105 loc) · 3.41 KB
/
error.rs
File metadata and controls
114 lines (105 loc) · 3.41 KB
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
//! Errors returned by the logger.
use std;
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum LoggerError {
/// First attempt at initialization failed.
NeverInitialized(String),
/// The logger does not allow reinitialization.
AlreadyInitialized,
/// Creating log file fails.
CreateLogFile(std::io::Error),
/// Writing to log file fails.
FileLogWrite(std::io::Error),
/// Flushing to disk fails.
FileLogFlush(std::io::Error),
/// Error obtaining lock on mutex.
FileLogLock(String),
}
impl fmt::Display for LoggerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let printable = match *self {
LoggerError::NeverInitialized(ref e) => format!("{}", e),
LoggerError::AlreadyInitialized => {
format!("{}", "Reinitialization of logger not allowed.")
}
LoggerError::CreateLogFile(ref e) => {
format!("Failed to create log file. Error: {}", e.description())
}
LoggerError::FileLogWrite(ref e) => {
format!("Failed to write to log file. Error: {}", e.description())
}
LoggerError::FileLogFlush(ref e) => {
format!("Failed to flush log file. Error: {}", e.description())
}
LoggerError::FileLogLock(ref e) => format!("{}", e),
};
write!(f, "{}", printable)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::ErrorKind;
#[test]
fn test_formatting() {
assert!(
format!(
"{:?}",
LoggerError::NeverInitialized(String::from("Bad Log Path Provided"))
).contains("NeverInitialized")
);
assert_eq!(
format!(
"{}",
LoggerError::NeverInitialized(String::from("Bad Log Path Provided"))
),
"Bad Log Path Provided"
);
assert!(format!("{:?}", LoggerError::AlreadyInitialized).contains("AlreadyInitialized"));
assert_eq!(
format!("{}", LoggerError::AlreadyInitialized),
"Reinitialization of logger not allowed."
);
assert!(
format!(
"{:?}",
LoggerError::FileLogWrite(std::io::Error::new(ErrorKind::Interrupted, "write"))
).contains("FileLogWrite")
);
assert_eq!(
format!(
"{}",
LoggerError::FileLogWrite(std::io::Error::new(ErrorKind::Interrupted, "write"))
),
"Failed to write to log file. Error: write"
);
assert!(
format!(
"{:?}",
LoggerError::FileLogFlush(std::io::Error::new(ErrorKind::Interrupted, "flush"))
).contains("FileLogFlush")
);
assert_eq!(
format!(
"{}",
LoggerError::FileLogFlush(std::io::Error::new(ErrorKind::Interrupted, "flush"))
),
"Failed to flush log file. Error: flush"
);
assert!(
format!(
"{:?}",
LoggerError::FileLogLock(String::from("File log lock"))
).contains("FileLogLock")
);
assert_eq!(
format!(
"{}",
LoggerError::FileLogLock(String::from("File log lock"))
),
"File log lock"
);
}
}