Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregating all your Exceptions #188

Open
A9G-Data-Droid opened this issue Jun 21, 2023 · 0 comments
Open

Aggregating all your Exceptions #188

A9G-Data-Droid opened this issue Jun 21, 2023 · 0 comments

Comments

@A9G-Data-Droid
Copy link

As a noob here, when I read the instructions in Example 3 - reading root cause from errors:

foreach(IError error in result.Errors)
{
    foreach(ExceptionalError causedByExceptionalError in error.Reasons.OfType<ExceptionalError>())
    {
        Console.WriteLine(causedByExceptionalError.Exception);
    }
}

I think that the .NET construct for a bunch of exceptions is the AggregateException

At first I used this:

IEnumerable<Exception> exceptionalErrors = error.Errors.SelectMany(err => err.Reasons.OfType<ExceptionalError>())
            .Select(ex => ex.Exception);

I don't want to repeat that code everywhere so I made an extension:

    /// <summary>
    /// Aggregates all of the Exceptions from all of the reasons behind all of the errors.
    /// </summary>
    /// <param name="error">When your <see cref="Result"/> ends in failure.</param>
    /// <returns>All of the exceptions that happened when processing the <see cref="Result"/></returns>
    public static AggregateException Exceptions<T>(this Result<T> error)
    {
        IEnumerable<Exception> exceptionalErrors = error.Errors.SelectMany(err => err.Reasons.OfType<ExceptionalError>())
            .Select(ex => ex.Exception);

        return new AggregateException(exceptionalErrors);
    }

Now when I'm interacting with a method that accepts just one Exception, I don't have to manually choose which one to pass in. I share the love and pass them all!

Is this a good feature for the library or did I miss something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant