[rs] Report out of memory errors

This commit is contained in:
Mikko Lehtonen
2020-09-14 23:26:00 +03:00
parent 3c0634fdec
commit 2e791cacde
2 changed files with 25 additions and 3 deletions

View File

@@ -1463,6 +1463,25 @@ where
fn unwrap_error_sink(self, error_sink: &ErrorSink, fallback: impl FnOnce() -> T) -> T {
self.unwrap_or_else(|err| {
let error_sink = error_sink.lock();
// Check to see if it is a out of memory error
let mut source_opt: Option<&(dyn std::error::Error + 'static)> = Some(&err);
while let Some(source) = source_opt {
if let Some(device_error) = source.downcast_ref::<wgc::device::DeviceError>() {
match device_error {
wgc::device::DeviceError::OutOfMemory => {
error_sink.handle_error(crate::Error::OutOfMemoryError {
source: Box::new(err)
});
return fallback();
},
_ => {}
}
}
source_opt = source.source();
}
// Otherwise, it is a validation error
error_sink.handle_error(crate::Error::ValidationError {
source: Box::new(err),
});

View File

@@ -2633,7 +2633,10 @@ impl<T> UncapturedErrorHandler for T where T: Fn(Error) + Send + Sync + 'static
#[derive(Debug)]
pub enum Error {
/// Out of memory error
OutOfMemoryError,
OutOfMemoryError {
///
source: Box<dyn error::Error + Send + Sync + 'static>,
},
/// Validation error, signifying a bug in code or data
ValidationError {
///
@@ -2644,7 +2647,7 @@ pub enum Error {
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::OutOfMemoryError => None,
Error::OutOfMemoryError { source } => Some(source.as_ref()),
Error::ValidationError { source } => Some(source.as_ref()),
}
}
@@ -2653,7 +2656,7 @@ impl error::Error for Error {
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::OutOfMemoryError => f.write_str("Out of Memory"),
Error::OutOfMemoryError { .. } => f.write_str("Out of Memory"),
Error::ValidationError { .. } => f.write_str("Validation error"),
}
}