use std::error::Error;
use ohno::OhnoCore;
#[ohno::error]
#[display("failed for {path}, carrying {carried}")]
pub struct DeclaredCoreError {
pub path: String,
pub carried: OhnoCore,
}
#[test]
fn declared_core_field_is_an_ordinary_field() {
let carried = OhnoCore::without_backtrace(std::io::Error::new(std::io::ErrorKind::NotFound, "carried.txt"));
let cause = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "root cause");
let error = DeclaredCoreError::caused_by("/etc/config.toml".to_string(), carried, cause);
let rendered = error.to_string();
assert!(
rendered.starts_with("failed for /etc/config.toml, carrying carried.txt"),
"got: {rendered}"
);
assert!(rendered.contains("caused by: root cause"), "got: {rendered}");
assert_eq!(error.source().expect("source").to_string(), "root cause");
assert_eq!(error.carried.source().expect("carried source").to_string(), "carried.txt");
}
#[test]
fn declared_core_field_does_not_take_over_the_error_representation() {
let carried = OhnoCore::without_backtrace(std::io::Error::new(std::io::ErrorKind::NotFound, "carried.txt"));
let error = DeclaredCoreError::new("/tmp/x".to_string(), carried);
assert!(error.source().is_none());
assert!(error.carried.source().is_some());
}
#[derive(ohno::Error)]
#[display("failed for {path}")]
pub struct PlacedCoreError {
pub path: String,
#[error]
inner: OhnoCore,
}
#[test]
fn suggested_remedy_places_the_core_by_hand() {
let cause = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "root cause");
let error = PlacedCoreError::caused_by("/etc/config.toml".to_string(), cause);
assert!(error.to_string().starts_with("failed for /etc/config.toml"), "got: {error}");
assert_eq!(error.source().expect("source").to_string(), "root cause");
}