forked from mirror/async-std
verbose errors feature
This adds a new "verbose-errors" feature flag to async-std that enables wrapping certain errors in structures with more context. As an example, we use it in `fs::File::{open,create}` to add the given path to the error message (something that is lacking in std to annoyance of many).new-scheduler
parent
355e2eded8
commit
8ce3e78952
@ -0,0 +1,51 @@
|
||||
use std::{error::Error, fmt, io};
|
||||
use crate::utils::VerboseErrorExt;
|
||||
|
||||
/// Wrap `std::io::Error` with additional message
|
||||
///
|
||||
/// *Note* Only active when `verbose-errors` feature is enabled for this crate!
|
||||
///
|
||||
/// Keeps the original error kind and stores the original I/O error as `source`.
|
||||
impl<T> VerboseErrorExt for Result<T, io::Error> {
|
||||
fn verbose_context(self, message: impl Fn() -> String) -> Self {
|
||||
if cfg!(feature = "verbose-errors") {
|
||||
self.map_err(|e| VerboseError::wrap(e, message()))
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct VerboseError {
|
||||
source: io::Error,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl VerboseError {
|
||||
fn wrap(source: io::Error, message: impl Into<String>) -> io::Error {
|
||||
io::Error::new(
|
||||
source.kind(),
|
||||
VerboseError {
|
||||
source,
|
||||
message: message.into(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VerboseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for VerboseError {
|
||||
fn description(&self) -> &str {
|
||||
self.source.description()
|
||||
}
|
||||
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
Some(&self.source)
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
mod verbose_tests {
|
||||
use async_std::{fs, task};
|
||||
|
||||
#[test]
|
||||
fn open_file() {
|
||||
task::block_on(async {
|
||||
let non_existing_file =
|
||||
"/ashjudlkahasdasdsikdhajik/asdasdasdasdasdasd/fjuiklashdbflasas";
|
||||
let res = fs::File::open(non_existing_file).await;
|
||||
match res {
|
||||
Ok(_) => panic!("Found file with random name: We live in a simulation"),
|
||||
Err(e) => assert_eq!(
|
||||
"Could not open /ashjudlkahasdasdsikdhajik/asdasdasdasdasdasd/fjuiklashdbflasas",
|
||||
&format!("{}", e)
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue