feat: automatic conversion from anyhow::Error and access to aggregate errors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-07-24 21:07:35 +02:00
parent 2bd9bd7b8e
commit a61f00a79d
2 changed files with 30 additions and 1 deletions

View File

@@ -30,11 +30,23 @@ pub enum MadError {
CloseNotDefined,
}
impl From<anyhow::Error> for MadError {
fn from(value: anyhow::Error) -> Self {
Self::Inner(value)
}
}
#[derive(Debug)]
pub struct AggregateError {
errors: Vec<MadError>,
}
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: (")?;

View File

@@ -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(())
}