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: Value will now throw when errors are present
The Value property is now properly encapsulated. When errors are present, the property getter will throw, similar to other properties like Errors or FirstError.

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
  • Loading branch information
feO2x committed May 9, 2024
commit 1f43ff314e0358efa0c55d0745380b5f8cac0d30
13 changes: 12 additions & 1 deletion src/ErrorOr/ErrorOr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@ private ErrorOr(TValue value)
/// <summary>
/// Gets the value.
/// </summary>
public TValue Value => _value!;
public TValue Value
{
get
{
if (IsError)
{
throw new InvalidOperationException("The Value property cannot be accessed when errors have been recorded. Check IsError before accessing Value.");
}

return _value;
}
}

/// <summary>
/// Gets the first error.
Expand Down
14 changes: 8 additions & 6 deletions tests/ErrorOr/ErrorOr.InstantiationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,18 @@ public void CreateFromErrorList_WhenAccessingErrorsOrEmptyList_ShouldReturnError
}

[Fact]
public void CreateFromErrorList_WhenAccessingValue_ShouldReturnDefault()
public void CreateFromErrorList_WhenAccessingValue_ShouldThrowInvalidOperationException()
{
// Arrange
List<Error> errors = new() { Error.Validation("User.Name", "Name is too short") };
ErrorOr<Person> errorOrPerson = ErrorOr<Person>.From(errors);

// Act
Person value = errorOrPerson.Value;
var act = () => errorOrPerson.Value;

// Assert
value.Should().Be(default);
act.Should().Throw<InvalidOperationException>()
.And.Message.Should().Be("The Value property cannot be accessed when errors have been recorded. Check IsError before accessing Value.");
}

[Fact]
Expand Down Expand Up @@ -247,16 +248,17 @@ public void ImplicitCastSingleError_WhenAccessingErrors_ShouldReturnErrorList()
}

[Fact]
public void ImplicitCastError_WhenAccessingValue_ShouldReturnDefault()
public void ImplicitCastError_WhenAccessingValue_ShouldThrowInvalidOperationException()
{
// Arrange
ErrorOr<Person> errorOrPerson = Error.Validation("User.Name", "Name is too short");

// Act
Person value = errorOrPerson.Value;
var act = () => errorOrPerson.Value;

// Assert
value.Should().Be(default);
act.Should().Throw<InvalidOperationException>()
.And.Message.Should().Be("The Value property cannot be accessed when errors have been recorded. Check IsError before accessing Value.");
}

[Fact]
Expand Down