Table of Contents

Class ExceptionDecoratorExtensions

Namespace
Cuemon
Assembly
Cuemon.Core.dll

Extension methods for the Exception class hidden behind the IDecorator<T> interface.

public static class ExceptionDecoratorExtensions
Inheritance
ExceptionDecoratorExtensions

Examples

ExceptionDecoratorExtensions provides extension methods on Decorator.Enclose for flattening deeply nested exception hierarchies into a flat sequence via the Flatten method. This example creates a three-level exception chain (InvalidOperationExceptionArgumentExceptionTimeoutException) and an AggregateException containing two root-level exceptions. Key steps include wrapping each exception with Decorator.Enclose, calling Flatten(), and materializing the result with ToList(). Console output shows the flattened chain order (InvalidOperationException -> ArgumentException -> TimeoutException) and the aggregate exception count, confirming the full hierarchy is unwound without losing any exceptions.

using System;
using System.Linq;
using Cuemon;

namespace Contoso.Diagnostics;

public sealed class ExceptionDecoratorExtensionsExample
{
    public static void Run()
    {
        Exception nested = new InvalidOperationException(
            "Request failed.",
            new ArgumentException("Endpoint is invalid.", new TimeoutException("The call timed out.")));

        var flattened = Decorator.Enclose(nested).Flatten().ToList();

        Exception aggregate = new AggregateException(
            new InvalidOperationException("Retry later."),
            new TimeoutException("The call timed out."));

        var aggregateInner = Decorator.Enclose(aggregate).Flatten().ToList();

        Console.WriteLine($"Nested chain: {string.Join(" -> ", flattened.Select(ex => ex.GetType().Name))}");
        Console.WriteLine($"Aggregate count: {aggregateInner.Count}");
    }
}

Methods

Flatten(IDecorator<Exception>)

Flattens any inner exceptions from the enclosed Exception of the decorator into an IEnumerable<T> sequence of exceptions.

public static IEnumerable<Exception> Flatten(this IDecorator<Exception> decorator)

Parameters

decorator IDecorator<Exception>

The IDecorator<T> to extend.

Returns

IEnumerable<Exception>

An empty IEnumerable<T> sequence if no inner exception(s) was specified; otherwise any inner exception(s) chained to the enclosed Exception of the decorator.

Remarks

If any inner exceptions are referenced, this method will iterative flatten them all from the enclosed Exception of the decorator.
Should the enclosed Exception of the decorator be of AggregateException, the return sequence of this method will be equal to the result of the InnerExceptions property after a call to Flatten().

Exceptions

ArgumentNullException

decorator cannot be null.

See Also