Skip to content

Commit

Permalink
Fix 181 (#182)
Browse files Browse the repository at this point in the history
* Make ErrorList correlationId optional
Allow Result.Error to take a single error message string

* Added unit tests
  • Loading branch information
ardalis authored May 16, 2024
1 parent e1efbd0 commit b28ce95
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Ardalis.Result/ErrorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ namespace Ardalis.Result;
/// </summary>
/// <param name="ErrorMessages"></param>
/// <param name="CorrelationId"></param>
public record ErrorList(IEnumerable<string> ErrorMessages, string? CorrelationId);
public record ErrorList(IEnumerable<string> ErrorMessages, string? CorrelationId = null);

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 10 in src/Ardalis.Result/ErrorList.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
11 changes: 11 additions & 0 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public static Result<T> Created(T value, string location)
return new Result<T>(ResultStatus.Created) { Value = value, Location = location };
}

/// <summary>
/// Represents an error that occurred during the execution of the service.
/// A single error message may be provided and will be exposed via the Errors property.
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static Result<T> Error(string errorMessage)
{
return new Result<T>(ResultStatus.Error) { Errors = new[] { errorMessage } };
}

/// <summary>
/// Represents an error that occurred during the execution of the service.
/// Error messages may be provided and will be exposed via the Errors property.
Expand Down
4 changes: 3 additions & 1 deletion tests/Ardalis.Result.UnitTests/PagedResultConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCall()
{
string errorMessage = "Something bad happened.";
string correlationId = Guid.NewGuid().ToString();
ErrorList errors = new(new[] { errorMessage }, correlationId);

var result = Result<object>
.Error(new([errorMessage], correlationId))
.Error(errors)
.ToPagedResult(_pagedInfo);

Assert.Equal(ResultStatus.Error, result.Status);
Expand Down
44 changes: 33 additions & 11 deletions tests/Ardalis.Result.UnitTests/ResultConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void InitializesValueUsingGenericFactoryMethodAndSetsStatusToOkWithMessag
Assert.Equal(value, result.Value);
Assert.Equal(message, result.SuccessMessage);
}

[Theory]
[InlineData(null)]
[InlineData(123)]
Expand All @@ -104,25 +104,25 @@ public void InitializesStatusToCreatedAndSetLocationGivenCreatedFactoryCall(obje
{
string location = "https://github.com/ardalis/Result";
var result = Result<object>.Created(value, location);

Assert.Equal(ResultStatus.Created, result.Status);
Assert.Equal(location, result.Location);
Assert.True(result.IsSuccess);
}

[Theory]
[InlineData(null)]
[InlineData(123)]
[InlineData("test value")]
public void InitializesStatusToCreatedGivenCreatedFactoryCall(object value)
{
var result = Result<object>.Created(value);

Assert.Equal(ResultStatus.Created, result.Status);
Assert.Equal(result.Location, string.Empty);
Assert.True(result.IsSuccess);
}

[Fact]
public void InitializesStatusToErrorGivenErrorFactoryCall()
{
Expand All @@ -131,16 +131,38 @@ public void InitializesStatusToErrorGivenErrorFactoryCall()
Assert.Equal(ResultStatus.Error, result.Status);
}

[Fact]
public void InitializesStatusToErrorGivenErrorFactoryCallWithSimpleMessage()
{
string errorMessage = Guid.NewGuid().ToString();
var result = Result<object>.Error(errorMessage);

Assert.Equal(ResultStatus.Error, result.Status);
Assert.Equal(errorMessage, result.Errors.Single());
}

[Fact]
public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCall()
{
string errorMessage = "Something bad happened.";
string correlationId = Guid.NewGuid().ToString();
var result = Result<object>.Error(new([errorMessage], correlationId));
ErrorList errors = new(new[] { errorMessage }, correlationId);
var result = Result<object>.Error(errors);

Assert.Equal(ResultStatus.Error, result.Status);
Assert.Equal(errorMessage, result.Errors.Single());
Assert.Equal(correlationId, result.CorrelationId);
}

[Fact]
public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCallWithoutCorrelationId()
{
string errorMessage = "Something bad happened.";
ErrorList errors = new(new[] { errorMessage });
var result = Result<object>.Error(errors);

Assert.Equal(ResultStatus.Error, result.Status);
Assert.Equal(errorMessage, result.Errors.Single());
Assert.Equal(correlationId, result.CorrelationId);
}

[Fact]
Expand Down Expand Up @@ -185,7 +207,7 @@ public void InitializesStatusToNotFoundGivenNotFoundFactoryCallWithString()
Assert.Equal(ResultStatus.NotFound, result.Status);
Assert.Equal(errorMessage, result.Errors.First());
}

[Fact]
public void InitializesStatusToConflictGivenConflictFactoryCall()
{
Expand Down Expand Up @@ -222,7 +244,7 @@ public void InitializesStatusToUnavailableGivenUnavailableFactoryCallWithString(
Assert.Equal(ResultStatus.Unavailable, result.Status);
Assert.Equal(errorMessage, result.Errors.First());
}


[Fact]
public void InitializedIsSuccessTrueForSuccessFactoryCall()
Expand Down Expand Up @@ -271,7 +293,7 @@ public void InitializedIsSuccessFalseForNotFoundFactoryCall()

Assert.False(result.IsSuccess);
}

[Fact]
public void InitializedIsSuccessFalseForConflictFactoryCall()
{
Expand All @@ -292,7 +314,7 @@ public void InitializedIsSuccessFalseForCriticalErrorFactoryCall()
public void InitializesStatusToNoContentForNoContentFactoryCall()
{
var result = Result<object>.NoContent();

Assert.True(result.IsSuccess);
}
}

0 comments on commit b28ce95

Please sign in to comment.