Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Aggregating

Errors that may contain multiple low-level errors should implement the IAggregateError interface.

It extends IError with an InnerErrors property, which unlike InnerError is a collection of errors, similar to InnerExceptions being the property of AggregateException.

All IAggregateError implementations should use the first element of InnerErrors as the value of InnerError, or null if InnerErrors is empty or null.

Unlike exceptions, the value of the InnerErrors can be null. It means that the error is not aggregated, but InnerError may be not null. To avoid handling it manually, use the GetInnerErrors() extension, which always returns a not null IError collection.

For manual aggregation, use the AggregateError implementation.

Usage example:

IAggregateError aggError = new AggregateError(
    "Aggregate error",
    new MessageError("Inner error 0"),
    new MessageError("Inner error 1"),
    new MessageError("Inner error 2")
);

IError[] innerErrors = aggError.InnerErrors!.ToArray();
Assert.Equal("Aggregate error", aggError.Message);
Assert.Equal("Inner error 0", innerErrors[0].Message);
Assert.Equal("Inner error 1", innerErrors[1].Message);
Assert.Equal("Inner error 2", innerErrors[2].Message);

GetInnerErrors example:

IError aggError = new AggregateError(
    "Aggregate error",
    new MessageError("Inner error 0"),
    new MessageError("Inner error 1"),
    new MessageError("Inner error 2")
);
IError notAggError = new MessageError("Not aggregate inner").Context("Not aggregate error");
IError innerEmptyError = new MessageError("Not inner errors");

Assert.Equal(3, aggError.GetInnerErrors().Count);
Assert.Equal(1, notAggError.GetInnerErrors().Count);
Assert.Equal(0, innerEmptyError.GetInnerErrors().Count);