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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::*;
use std::convert::TryInto;
use bindings::{
windows::win32::automation::{GetErrorInfo, SetErrorInfo, BSTR},
windows::win32::winrt::{ILanguageExceptionErrorInfo2, IRestrictedErrorInfo},
};
#[derive(Clone, PartialEq)]
pub struct Error {
code: ErrorCode,
info: Option<IRestrictedErrorInfo>,
}
impl Error {
pub fn new(code: ErrorCode, message: &str) -> Self {
let message: HString = message.into();
unsafe {
let _ = RoOriginateError(code, message.abi() as _);
}
let mut info = None;
let info = unsafe {
GetErrorInfo(0, &mut info)
.and_some(info)
.and_then(|e| e.cast())
.ok()
};
Self { code, info }
}
pub fn fast_error(code: ErrorCode) -> Self {
Self { code, info: None }
}
pub fn code(&self) -> ErrorCode {
self.code
}
pub fn info(&self) -> &Option<IRestrictedErrorInfo> {
&self.info
}
pub fn message(&self) -> String {
if let Some(info) = &self.info {
let mut fallback = BSTR::default();
let mut message = BSTR::default();
let mut unused = BSTR::default();
let mut code = ErrorCode(0);
unsafe {
let _ = info.GetErrorDetails(&mut fallback, &mut code, &mut message, &mut unused);
}
let message = if !message.is_empty() {
message
} else {
fallback
};
let message: String = message.try_into().unwrap_or_default();
if self.code == code {
return message.trim_end().to_owned();
}
}
self.code.message()
}
}
impl std::convert::From<Error> for ErrorCode {
fn from(error: Error) -> Self {
let code = error.code;
let info = error.info.and_then(|info| info.cast().ok());
unsafe {
let _ = SetErrorInfo(0, info);
}
code
}
}
impl std::convert::From<ErrorCode> for Error {
fn from(code: ErrorCode) -> Self {
let mut info = None;
let info: Option<IRestrictedErrorInfo> = unsafe {
GetErrorInfo(0, &mut info)
.and_some(info)
.and_then(|e| e.cast())
.ok()
};
if let Some(info) = info {
if let Ok(capture) = info.cast::<ILanguageExceptionErrorInfo2>() {
unsafe {
let _ = capture.CapturePropagationContext(None);
}
}
return Self {
code,
info: Some(info),
};
}
let mut result = None;
unsafe {
let _ = GetErrorInfo(0, &mut result);
}
if let Some(info) = result {
let mut message = BSTR::default();
unsafe {
let _ = info.GetDescription(&mut message);
}
let message: String = message.try_into().unwrap_or_default();
Self::new(code, &message)
} else {
Self::new(code, "")
}
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("Error")
.field("code", &format_args!("{:#010X}", self.code.0))
.field("message", &self.message())
.finish()
}
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.write_str(&self.message())
}
}
impl std::error::Error for Error {}
demand_load! {
"combase.dll" {
fn RoOriginateError(code: ErrorCode, message: RawPtr) -> i32;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message() {
let code = Error::fast_error(ErrorCode::from_win32(0));
assert_eq!(code.message(), "The operation completed successfully.");
let code = Error::fast_error(ErrorCode::from_win32(997));
assert_eq!(code.message(), "Overlapped I/O operation is in progress.");
}
}