Skip to content

Commit

Permalink
feat: add methods to append errors from list of ErrorOr to current
Browse files Browse the repository at this point in the history
wip, this is only the basic implementation.
ToDo:
- AppendErrors with ErrorOrs of same type using generics
- AppendErrors to `Task<ErrorOr>`
- Tests :)

Refs: amantinband#120
  • Loading branch information
davidferneding committed Nov 15, 2024
1 parent bd91f89 commit cda0fd6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ErrorOr/ErrorOr.AppendErrors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ErrorOr;

public readonly partial record struct ErrorOr<TValue> : IErrorOr<TValue>
{
/// <summary>
/// If the state is error or any of the given <paramref name="errorOrs"/> has errors, an <see cref="ErrorOr"/> instance with all errors of the given instances will be returned.
/// </summary>
/// <param name="errorOrs">The <see cref="ErrorOr"/> instances which errors should be added to the result.</param>
/// <returns>The original <see cref="Value"/> if all states are value, otherwise a list of all errors.</returns>
public ErrorOr<TValue> AppendErrors(params IErrorOr[] errorOrs)
{
List<Error> combinedErrors = ErrorsOrEmptyList;
foreach (IErrorOr errorOr in errorOrs)
{
combinedErrors.AddRange(errorOr.Errors ?? []);
}

return combinedErrors.Count == 0 ? this : combinedErrors;
}
}
21 changes: 21 additions & 0 deletions src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// If the state of <paramref name="original"/> is error or any of the given <paramref name="errorOrs"/> has errors, an <see cref="ErrorOr"/> instance with all errors of the given instances will be returned.
/// </summary>
/// <param name="original">The <see cref="ErrorOr"/> instance which value should be returned on success.</param>
/// <param name="errorOrs">The <see cref="ErrorOr"/> instances which errors should be added to the result.</param>
/// <returns>The value of <paramref name="original"/> if all states are value, otherwise a list of all errors.</returns>
public static ErrorOr<TValue> AppendErrors<TValue>(this ErrorOr<TValue> original, params IErrorOr[] errorOrs)
{
List<Error> combinedErrors = original.ErrorsOrEmptyList;
foreach (IErrorOr errorOr in errorOrs)
{
combinedErrors.AddRange(errorOr.Errors ?? []);
}

return combinedErrors.Count == 0 ? original : combinedErrors;
}
}

0 comments on commit cda0fd6

Please sign in to comment.