From a61f00a79d4af6771544cb4e4beb16b3bbdc3be9 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Thu, 24 Jul 2025 21:07:35 +0200 Subject: [PATCH] feat: automatic conversion from anyhow::Error and access to aggregate errors --- crates/mad/src/lib.rs | 12 ++++++++++++ crates/mad/tests/mod.rs | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/mad/src/lib.rs b/crates/mad/src/lib.rs index dcd9c07..85e46ff 100644 --- a/crates/mad/src/lib.rs +++ b/crates/mad/src/lib.rs @@ -30,11 +30,23 @@ pub enum MadError { CloseNotDefined, } +impl From for MadError { + fn from(value: anyhow::Error) -> Self { + Self::Inner(value) + } +} + #[derive(Debug)] pub struct AggregateError { errors: Vec, } +impl AggregateError { + pub fn get_errors(&self) -> &[MadError] { + &self.errors + } +} + impl Display for AggregateError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("MadError::AggregateError: (")?; diff --git a/crates/mad/tests/mod.rs b/crates/mad/tests/mod.rs index 81bef33..f88e1bd 100644 --- a/crates/mad/tests/mod.rs +++ b/crates/mad/tests/mod.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use async_trait::async_trait; -use notmad::{Component, Mad}; +use notmad::{Component, Mad, MadError}; use rand::Rng; use tokio::sync::Mutex; use tokio_util::sync::CancellationToken; @@ -137,3 +137,20 @@ async fn test_can_shutdown_gracefully() -> anyhow::Result<()> { Ok(()) } + +#[test] +fn test_can_easily_transform_error() -> anyhow::Result<()> { + fn fallible() -> anyhow::Result<()> { + Ok(()) + } + + fn inner() -> Result<(), MadError> { + fallible()?; + + Ok(()) + } + + inner()?; + + Ok(()) +}