ponsic_winsafe/win/
error.rs

1use winapi::um::errhandlingapi::GetLastError;
2
3use super::gen_by_py::translate_error;
4
5/// WIN32 错误
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct SystemError {
8    pub code: u32,
9    pub message: &'static str,
10}
11
12impl std::fmt::Display for SystemError {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        write!(f, "系统错误: {}", self.message)
15    }
16}
17
18impl std::error::Error for SystemError {}
19
20impl SystemError {
21    pub fn new(code: u32) -> Self {
22        Self {
23            code,
24            message: translate_error(code),
25        }
26    }
27}
28
29/// 检查当前上下文中的错误代码
30pub fn check_error() -> Result<(), SystemError> {
31    let code = unsafe { GetLastError() };
32    if code != 0 {
33        return Err(SystemError::new(code));
34    }
35    Ok(())
36}