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

Exceptions for invalid use - review and extensions #106

Prev Previous commit
Next Next commit
feat: ArgumentException is now thrown when empty errors list/array is…
… passed

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
  • Loading branch information
feO2x committed May 9, 2024
commit 3359933480e7234c962b9ffee6c92e2faf2926c8
8 changes: 4 additions & 4 deletions src/ErrorOr/ErrorOr.ImplicitConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static implicit operator ErrorOr<TValue>(Error error)
/// Creates an <see cref="ErrorOr{TValue}"/> from a list of errors.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="errors"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="errors" /> is an empty list.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="errors" /> is an empty list.</exception>
public static implicit operator ErrorOr<TValue>(List<Error> errors)
{
if (errors is null)
Expand All @@ -32,7 +32,7 @@ public static implicit operator ErrorOr<TValue>(List<Error> errors)

if (errors.Count == 0)
{
throw new InvalidOperationException("Cannot create an ErrorOr<TValue> from an empty list of errors. Provide at least one error.");
throw new ArgumentException("Cannot create an ErrorOr<TValue> from an empty list of errors. Provide at least one error.", nameof(errors));
}

return new ErrorOr<TValue>(errors);
Expand All @@ -42,7 +42,7 @@ public static implicit operator ErrorOr<TValue>(List<Error> errors)
/// Creates an <see cref="ErrorOr{TValue}"/> from a list of 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>
/// <exception cref="ArgumentException">Thrown when <paramref name="errors" /> is an empty array.</exception>
public static implicit operator ErrorOr<TValue>(Error[] errors)
{
if (errors is null)
Expand All @@ -52,7 +52,7 @@ public static implicit operator ErrorOr<TValue>(Error[] errors)

if (errors.Length == 0)
{
throw new InvalidOperationException("Cannot create an ErrorOr<TValue> from an empty array of errors. Provide at least one error.");
throw new ArgumentException("Cannot create an ErrorOr<TValue> from an empty array of errors. Provide at least one error.", nameof(errors));
}

return new ErrorOr<TValue>(errors.ToList());
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorOr/ErrorOr.ToErrorOrExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static ErrorOr<TValue> ToErrorOr<TValue>(this Error error)
/// 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 list.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="errors" /> is an empty list.</exception>
public static ErrorOr<TValue> ToErrorOr<TValue>(this List<Error> errors)
{
return errors;
Expand All @@ -32,7 +32,7 @@ public static ErrorOr<TValue> ToErrorOr<TValue>(this List<Error> errors)
/// 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>
/// <exception cref="ArgumentException">Thrown when <paramref name="errors" /> is an empty array.</exception>
public static ErrorOr<TValue> ToErrorOr<TValue>(this Error[] errors)
{
return errors;
Expand Down
8 changes: 6 additions & 2 deletions tests/ErrorOr/ErrorOr.InstantiationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ public void CreateErrorOr_WhenEmptyErrorsList_ShouldThrow()
Func<ErrorOr<int>> errorOrInt = () => new List<Error>();

// Assert
errorOrInt.Should().ThrowExactly<InvalidOperationException>();
var exception = errorOrInt.Should().ThrowExactly<ArgumentException>().Which;
exception.Message.Should().Be("Cannot create an ErrorOr<TValue> from an empty list of errors. Provide at least one error. (Parameter 'errors')");
exception.ParamName.Should().Be("errors");
}

[Fact]
Expand All @@ -374,7 +376,9 @@ public void CreateErrorOr_WhenEmptyErrorsArray_ShouldThrow()
Func<ErrorOr<int>> errorOrInt = () => Array.Empty<Error>();

// Assert
errorOrInt.Should().ThrowExactly<InvalidOperationException>();
var exception = errorOrInt.Should().ThrowExactly<ArgumentException>().Which;
exception.Message.Should().Be("Cannot create an ErrorOr<TValue> from an empty array of errors. Provide at least one error. (Parameter 'errors')");
exception.ParamName.Should().Be("errors");
}

[Fact]
Expand Down