[rs] impl std error for Error

Relies more on the source() of error for informative messages
This commit is contained in:
Mikko Lehtonen
2020-09-05 01:29:03 +03:00
parent 4277bcab2c
commit e28bc9cbbb
2 changed files with 26 additions and 9 deletions

View File

@@ -1410,7 +1410,9 @@ 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();
error_sink.handle_error(crate::Error::from(err));
error_sink.handle_error(crate::Error::ValidationError {
source: Box::new(err),
});
fallback()
})
}
@@ -1418,12 +1420,6 @@ where
type ErrorSink = Arc<Mutex<ErrorSinkRaw>>;
impl<E: Error + Send + Sync + 'static> From<E> for crate::Error {
fn from(err: E) -> Self {
crate::Error::ValidationError { source: err.into() }
}
}
struct ErrorSinkRaw {
uncaptured_handler: Box<dyn crate::UncapturedErrorHandler>,
}
@@ -1446,6 +1442,17 @@ impl Debug for ErrorSinkRaw {
}
fn default_error_handler(err: crate::Error) {
eprintln!("WGPU Error: {}", err);
eprintln!("wgpu error: {}\n", err);
if err.source().is_some() {
eprintln!("Caused by:");
let mut source_opt = err.source();
while let Some(source) = source_opt {
eprintln!(" {}", source);
source_opt = source.source();
}
eprintln!();
}
panic!("Handling wgpu errors as fatal by default");
}

View File

@@ -2636,11 +2636,21 @@ pub enum Error {
source: Box<dyn error::Error + Send + Sync + 'static>,
},
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::OutOfMemoryError => None,
Error::ValidationError { source } => Some(source.as_ref()),
}
}
}
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::ValidationError { source } => std::fmt::Display::fmt(source, f),
Error::ValidationError { .. } => f.write_str("Validation error"),
}
}
}