diff --git a/src/ErrorOr/ErrorOr.AppendErrors.cs b/src/ErrorOr/ErrorOr.AppendErrors.cs new file mode 100644 index 0000000..136a460 --- /dev/null +++ b/src/ErrorOr/ErrorOr.AppendErrors.cs @@ -0,0 +1,20 @@ +namespace ErrorOr; + +public readonly partial record struct ErrorOr : IErrorOr +{ + /// + /// If the state is error or any of the given has errors, an instance with all errors of the given instances will be returned. + /// + /// The instances which errors should be added to the result. + /// The original if all states are value, otherwise a list of all errors. + public ErrorOr AppendErrors(params IErrorOr[] errorOrs) + { + List combinedErrors = ErrorsOrEmptyList; + foreach (IErrorOr errorOr in errorOrs) + { + combinedErrors.AddRange(errorOr.Errors ?? []); + } + + return combinedErrors.Count == 0 ? this : combinedErrors; + } +} diff --git a/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs b/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs new file mode 100644 index 0000000..c61f269 --- /dev/null +++ b/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs @@ -0,0 +1,21 @@ +namespace ErrorOr; + +public static partial class ErrorOrExtensions +{ + /// + /// If the state of is error or any of the given has errors, an instance with all errors of the given instances will be returned. + /// + /// The instance which value should be returned on success. + /// The instances which errors should be added to the result. + /// The value of if all states are value, otherwise a list of all errors. + public static ErrorOr AppendErrors(this ErrorOr original, params IErrorOr[] errorOrs) + { + List combinedErrors = original.ErrorsOrEmptyList; + foreach (IErrorOr errorOr in errorOrs) + { + combinedErrors.AddRange(errorOr.Errors ?? []); + } + + return combinedErrors.Count == 0 ? original : combinedErrors; + } +}