Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Migrate from Errors to Exceptions for invalid use of library #105

Merged
merged 13 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add ToErrorOr extension method for error arrays
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
  • Loading branch information
feO2x committed May 9, 2024
commit 5298abc4558564d5b3637ffd90db222a9b13d056
10 changes: 10 additions & 0 deletions src/ErrorOr/ErrorOr.ToErrorOrExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public static ErrorOr<TValue> ToErrorOr<TValue>(this List<Error> errors)
{
return errors;
}

/// <summary>
/// Creates an <see cref="ErrorOr{TValue}"/> instance with the given <paramref name="errors"/>.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="errors"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="errors" /> is an empty array.</exception>
public static ErrorOr<TValue> ToErrorOr<TValue>(this Error[] errors)
{
return errors;
}
}
11 changes: 11 additions & 0 deletions tests/ErrorOr/ErrorOr.ToErrorOrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ public void ListOfErrorsToErrorOr_WhenAccessingErrors_ShouldReturnSameErrors()
result.IsError.Should().BeTrue();
result.Errors.Should().BeEquivalentTo(errors);
}

[Fact]
public void ArrayOfErrorsToErrorOr_WhenAccessingErrors_ShouldReturnSimilarErrors()
{
Error[] errors = [Error.Unauthorized(), Error.Validation()];

ErrorOr<int> result = errors.ToErrorOr<int>();

result.IsError.Should().BeTrue();
result.Errors.Should().Equal(errors);
}
}