use std::error::Error as StdError;
use std::io;
#[derive(Debug)]
pub struct GoWrapped {
prefix: String,
source: Box<dyn StdError + Send + Sync>,
}
impl GoWrapped {
pub fn new(
prefix: impl Into<String>,
source: impl Into<Box<dyn StdError + Send + Sync>>,
) -> Self {
Self {
prefix: prefix.into(),
source: source.into(),
}
}
}
impl std::fmt::Display for GoWrapped {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.prefix, self.source)
}
}
impl StdError for GoWrapped {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(self.source.as_ref())
}
}
pub fn go_errno_text(err: &io::Error) -> String {
let Some(code) = err.raw_os_error() else {
return err.to_string();
};
let text = io::Error::from_raw_os_error(code).to_string();
let text = match text.rfind(" (os error ") {
Some(i) => &text[..i],
None => text.as_str(),
};
let mut chars = text.chars();
match chars.next() {
Some(first) => first.to_lowercase().chain(chars).collect(),
None => String::new(),
}
}
#[cfg(test)]
mod tests {
use std::error::Error as StdError;
use std::io;
use super::{GoWrapped, go_errno_text};
#[derive(Debug)]
struct Cause;
impl std::fmt::Display for Cause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("root cause")
}
}
impl StdError for Cause {}
#[test]
fn display_is_prefix_colon_source() {
assert_eq!(
GoWrapped::new("failed to get session", Cause).to_string(),
"failed to get session: root cause"
);
assert_eq!(GoWrapped::new("", "x").to_string(), ": x");
let nested = GoWrapped::new("outer", GoWrapped::new("inner", "leaf"));
assert_eq!(nested.to_string(), "outer: inner: leaf");
}
#[test]
fn source_is_the_wrapped_cause() {
let err = GoWrapped::new("outer", GoWrapped::new("inner", Cause));
let inner = err.source().expect("outer has a source");
assert_eq!(inner.to_string(), "inner: root cause");
assert!(inner.is::<GoWrapped>());
let root = inner.source().expect("inner has a source");
assert!(root.is::<Cause>());
assert!(root.source().is_none());
}
#[test]
fn is_send_sync_static() {
fn assert_bounds<T: StdError + Send + Sync + 'static>() {}
assert_bounds::<GoWrapped>();
}
#[test]
fn errno_text_is_lowercased_strerror() {
assert_eq!(
go_errno_text(&io::Error::from_raw_os_error(2)),
"no such file or directory"
);
assert_eq!(
go_errno_text(&io::Error::from_raw_os_error(13)),
"permission denied"
);
assert_eq!(
go_errno_text(&io::Error::from_raw_os_error(21)),
"is a directory"
);
assert_eq!(go_errno_text(&io::Error::other("custom")), "custom");
}
}