summaryrefslogtreecommitdiff
path: root/engine-ecs/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine-ecs/src/error.rs')
-rw-r--r--engine-ecs/src/error.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/engine-ecs/src/error.rs b/engine-ecs/src/error.rs
index 2654dde..5ce5632 100644
--- a/engine-ecs/src/error.rs
+++ b/engine-ecs/src/error.rs
@@ -44,6 +44,44 @@ impl Error
{
Self { inner: self.inner.context(context) }
}
+
+ /// Retrieves the message text of a system-defined Windows Win32 error code and
+ /// returns it as a `Error`. If this fails (for example if the error code is invalid),
+ /// the returned `Error` contains the error code number.
+ #[cfg(windows)]
+ pub fn from_system_defined_win32_error(error: u32) -> Self
+ {
+ use std::ffi::CStr;
+ use std::ptr::null;
+
+ use windows_sys::Win32::System::Diagnostics::Debug::{
+ FormatMessageA,
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ };
+
+ let mut buffer = vec![0u8; 256];
+
+ let len = unsafe {
+ FormatMessageA(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ null(),
+ error,
+ 0,
+ buffer.as_mut_ptr(),
+ (buffer.len() - 1) as u32,
+ null(),
+ )
+ } as usize;
+
+ if len == 0 {
+ return Self::message(format!("Win32 error {error}"));
+ }
+
+ let buf = unsafe { CStr::from_bytes_with_nul_unchecked(&buffer[..len + 1]) };
+
+ Self::message(buf.to_string_lossy().trim().to_owned())
+ }
}
impl Debug for Error