-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patherror.rs
112 lines (94 loc) · 2.63 KB
/
error.rs
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
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt::Error as FmtError;
use std::str::Utf8Error;
use rusoto_core::ParseRegionError;
use rusoto_kms::{DecryptError, EncryptError};
use rustc_serialize::base64::FromBase64Error;
use serde_json::Error as SerdeJsonError;
pub struct KawsError {
message: String,
stderr: Option<String>,
stdout: Option<String>,
}
impl KawsError {
pub fn new(message: String) -> KawsError {
KawsError {
message: message,
stderr: None,
stdout: None,
}
}
pub fn with_std_streams(message: String, stdout: String, stderr: String) -> KawsError {
KawsError {
message: message,
stderr: Some(stderr),
stdout: Some(stdout),
}
}
}
impl Debug for KawsError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "{:?}", self.message)
}
}
impl Display for KawsError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
if self.stdout.is_some() && self.stderr.is_some() {
write!(f,
"{}
Standard streams from the underlying command that failed:
stdout:
{}
stderr:
{}",
self.message,
self.stdout.as_ref().expect("accessing self.stdout"),
self.stderr.as_ref().expect("accessing self.stderr")
)
} else {
write!(f, "{}", self.message)
}
}
}
impl Error for KawsError {
fn description(&self) -> &str {
&self.message
}
}
impl From<::std::io::Error> for KawsError {
fn from(error: ::std::io::Error) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<Utf8Error> for KawsError {
fn from(error: Utf8Error) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<DecryptError> for KawsError {
fn from(error: DecryptError) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<EncryptError> for KawsError {
fn from(error: EncryptError) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<FromBase64Error> for KawsError {
fn from(error: FromBase64Error) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<ParseRegionError> for KawsError {
fn from(error: ParseRegionError) -> Self {
KawsError::new(format!("{}", error))
}
}
impl From<SerdeJsonError> for KawsError {
fn from(error: SerdeJsonError) -> Self {
KawsError::new(format!("{}", error))
}
}
pub type KawsResult = Result<Option<String>, KawsError>;