forked from amantinband/error-or
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add methods to append errors from list of ErrorOr to current
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
1 parent
bd91f89
commit cda0fd6
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |