forked from mirror/async-std
Remove verbose-errors cargo feature
parent
99ddfb3f93
commit
c704643296
@ -1,52 +1,46 @@
|
||||
use crate::utils::VerboseErrorExt;
|
||||
use crate::utils::Context;
|
||||
|
||||
/// 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, std::io::Error> {
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
fn verbose_context(self, message: impl Fn() -> String) -> Self {
|
||||
self.map_err(|e| verbose::Error::wrap(e, message()))
|
||||
impl<T> Context for Result<T, std::io::Error> {
|
||||
fn context(self, message: impl Fn() -> String) -> Self {
|
||||
self.map_err(|e| VerboseError::wrap(e, message()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
mod verbose {
|
||||
use std::{error::Error as StdError, fmt, io};
|
||||
use std::{error::Error as StdError, fmt, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Error {
|
||||
source: io::Error,
|
||||
message: String,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct VerboseError {
|
||||
source: io::Error,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub(crate) fn wrap(source: io::Error, message: impl Into<String>) -> io::Error {
|
||||
io::Error::new(
|
||||
source.kind(),
|
||||
Error {
|
||||
source,
|
||||
message: message.into(),
|
||||
},
|
||||
)
|
||||
}
|
||||
impl VerboseError {
|
||||
pub(crate) 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 Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
impl fmt::Display for VerboseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
self.source.description()
|
||||
}
|
||||
impl StdError for VerboseError {
|
||||
fn description(&self) -> &str {
|
||||
self.source.description()
|
||||
}
|
||||
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
Some(&self.source)
|
||||
}
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
Some(&self.source)
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,16 @@
|
||||
#[cfg(feature = "verbose-errors")]
|
||||
mod verbose_tests {
|
||||
use async_std::{fs, task};
|
||||
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)
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
#[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