public static void Add(this IList <CommandError> errors, Exception exception, CommandErrorSeverity severity = CommandErrorSeverity.Error) { if (errors is null) { throw new ArgumentNullException(nameof(errors)); } if (exception is null) { throw new ArgumentNullException(nameof(exception)); } if (exception is AggregateException aggregateException) { foreach (var innerException in aggregateException.Flatten().InnerExceptions) { errors.Add(innerException, severity); } } else { errors.Add(new CommandError() { Message = exception.Message, Severity = severity }); } }
public static Exception ToException(this IEnumerable <CommandError> errors, CommandErrorSeverity minSeverity = CommandErrorSeverity.Error) { if (errors is null) { throw new ArgumentNullException(nameof(errors)); } var affectedErrors = errors .Where(error => error.Severity >= minSeverity); if (affectedErrors.Skip(1).Any()) { return(new AggregateException(affectedErrors.Select(error => error.ToException()))); } return(affectedErrors.SingleOrDefault()?.ToException()); }