Skip to content

Commit

Permalink
Update Error usage to utilize ErrorList record with ErrorMessages and…
Browse files Browse the repository at this point in the history
… CorrelationId (#173)

* Update Error usage to take in a strongly typed wrapper class, ErrorList, for ErrorMessages and CorrelationId

* Add default value for simpler usage of Error method

* Add XML comments

---------

Co-authored-by: Steve Smith <[email protected]>
  • Loading branch information
KyleMcMaster and ardalis authored May 15, 2024
1 parent 54b5de8 commit 51f2b90
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 42 deletions.
10 changes: 10 additions & 0 deletions src/Ardalis.Result/ErrorList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Ardalis.Result;

/// <summary>
/// A wrapper class for a list of error messages and an optional CorrelationId.
/// </summary>
/// <param name="ErrorMessages"></param>
/// <param name="CorrelationId"></param>
public record ErrorList(IEnumerable<string> ErrorMessages, string? CorrelationId);

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.
27 changes: 7 additions & 20 deletions src/Ardalis.Result/Result.Void.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,15 @@ public static Result<T> Success<T>(T value, string successMessage)
/// Represents an error that occurred during the execution of the service.
/// Error messages may be provided and will be exposed via the Errors property.
/// </summary>
/// <param name="errorMessages">A list of string error messages.</param>
/// <returns>A Result</returns>
public new static Result Error(params string[] errorMessages)
{
return new Result(ResultStatus.Error) { Errors = errorMessages };
}

/// <summary>
/// Represents an error that occurred during the execution of the service.
/// Sets the CorrelationId property to the provided value
/// Error messages may be provided and will be exposed via the Errors property.
/// </summary>
/// <param name="correlationId">Sets the CorrelationId property.</param>
/// <param name="errorMessages">A list of string error messages.</param>
/// <param name="error">An optional instance of ErrorList with list of string error messages and CorrelationId.</param>
/// <returns>A Result</returns>
public static Result ErrorWithCorrelationId(string correlationId, params string[] errorMessages)
public new static Result Error(ErrorList error = null)
{
return new Result(ResultStatus.Error)
{
CorrelationId = correlationId,
Errors = errorMessages
};
return new Result(ResultStatus.Error)
{
CorrelationId = error?.CorrelationId ?? string.Empty,
Errors = error?.ErrorMessages ?? []
};
}

/// <summary>
Expand Down
11 changes: 8 additions & 3 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

namespace Ardalis.Result
Expand Down Expand Up @@ -133,11 +134,15 @@ public static Result<T> Created(T value, string location)
/// Represents an error that occurred during the execution of the service.
/// Error messages may be provided and will be exposed via the Errors property.
/// </summary>
/// <param name="errorMessages">A list of string error messages.</param>
/// <param name="error">An optional instance of ErrorList with list of string error messages and CorrelationId.</param>
/// <returns>A Result<typeparamref name="T"/></returns>
public static Result<T> Error(params string[] errorMessages)
public static Result<T> Error(ErrorList error = null)
{
return new Result<T>(ResultStatus.Error) { Errors = errorMessages };
return new Result<T>(ResultStatus.Error)
{
CorrelationId = error?.CorrelationId ?? string.Empty,
Errors = error?.ErrorMessages ?? []
};
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Ardalis.Result/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Result<TDestination> Map<TSource, TDestination>(this Result<TSourc
case ResultStatus.Unauthorized: return Result<TDestination>.Unauthorized();
case ResultStatus.Forbidden: return Result<TDestination>.Forbidden();
case ResultStatus.Invalid: return Result<TDestination>.Invalid(result.ValidationErrors);
case ResultStatus.Error: return Result<TDestination>.Error(result.Errors.ToArray());
case ResultStatus.Error: return Result<TDestination>.Error(new ErrorList(result.Errors.ToArray(), result.CorrelationId));
case ResultStatus.Conflict: return result.Errors.Any()
? Result<TDestination>.Conflict(result.Errors.ToArray())
: Result<TDestination>.Conflict();
Expand Down
7 changes: 5 additions & 2 deletions tests/Ardalis.Result.UnitTests/PagedResultConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -94,12 +95,14 @@ public void InitializesStatusToErrorGivenErrorFactoryCall()
public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCall()
{
string errorMessage = "Something bad happened.";
string correlationId = Guid.NewGuid().ToString();
var result = Result<object>
.Error(errorMessage)
.Error(new([errorMessage], correlationId))
.ToPagedResult(_pagedInfo);

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

Expand Down
17 changes: 10 additions & 7 deletions tests/Ardalis.Result.UnitTests/ResultConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand All @@ -7,6 +8,10 @@ namespace Ardalis.Result.UnitTests;

public class ResultConstructor
{
private class TestObject
{
}

[Fact]
public void InitializesStronglyTypedStringValue()
{
Expand Down Expand Up @@ -34,10 +39,6 @@ public void InitializesStronglyTypedObjectValue()
Assert.Equal(expectedObject, result.Value);
}

private class TestObject
{
}

[Fact]
public void InitializesValueToNullGivenNullConstructorArgument()
{
Expand Down Expand Up @@ -134,10 +135,12 @@ public void InitializesStatusToErrorGivenErrorFactoryCall()
public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCall()
{
string errorMessage = "Something bad happened.";
var result = Result<object>.Error(errorMessage);
string correlationId = Guid.NewGuid().ToString();
var result = Result<object>.Error(new([errorMessage], correlationId));

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

[Fact]
Expand Down Expand Up @@ -232,7 +235,7 @@ public void InitializedIsSuccessTrueForSuccessFactoryCall()
[Fact]
public void InitializedIsSuccessFalseForErrorFactoryCall()
{
var result = Result<object>.Error(null);
var result = Result<object>.Error();

Assert.False(result.IsSuccess);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Ardalis.Result.UnitTests/ResultMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ public void ShouldProduceInvalidWithoutValidationErrors()
[Fact]
public void ShouldProduceErrorResultWithErrors()
{
var errors = new string[] { "Error 1", "Error 2" };
var result = Result<int>.Error(errors);
var errorList = new ErrorList(["Error 1", "Error 2"], default);
var result = Result<int>.Error(errorList);

var actual = result.Map(val => val.ToString());

actual.Status.Should().Be(ResultStatus.Error);
actual.Errors.Should().BeEquivalentTo(errors);
actual.Errors.Should().BeEquivalentTo(errorList.ErrorMessages);
}

[Fact]
public void ShouldProduceErrorResultWithNoErrors()
{
var errors = Array.Empty<string>();
var result = Result<int>.Error(errors);
var result = Result<int>.Error();

var actual = result.Map(val => val.ToString());

actual.Status.Should().Be(ResultStatus.Error);
actual.Errors.Should().BeEquivalentTo(errors);
actual.Errors.Should().BeEmpty();
actual.CorrelationId.Should().BeEmpty();
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions tests/Ardalis.Result.UnitTests/ResultVoidConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void InitializesSuccessResultWithMessageWithFactoryMethod()
[InlineData("test1", "test2")]
public void InitializesErrorResultWithFactoryMethod(params string[] errors)
{
var result = Result.Error(errors);
var result = Result.Error(new ErrorList(errors, null));

Assert.Null(result.Value);
Assert.Equal(ResultStatus.Error, result.Status);
Expand All @@ -59,7 +59,7 @@ public void InitializesErrorResultWithCorrelationIdWithFactoryMethod()
{
var correlationId = "testId";
var errors = new string[] { "Error 1", "Error 2" };
var result = Result.ErrorWithCorrelationId(correlationId, errors);
var result = Result.Error(new ErrorList(errors, correlationId));

Assert.Null(result.Value);
Assert.Equal(ResultStatus.Error, result.Status);
Expand Down
4 changes: 3 additions & 1 deletion tests/Ardalis.Result.UnitTests/ResultVoidToResultOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public class ResultVoidToResultOfT
[InlineData("test1", "test2")]
public void ConvertFromErrorResultOfUnit(params string[] errors)
{
var result = DoBusinessOperationExample<object>(Result.Error(errors));
var errorList = new ErrorList(errors, default);
var result = DoBusinessOperationExample<object>(Result.Error(errorList));

Assert.Null(result.Value);
Assert.Equal(ResultStatus.Error, result.Status);
Assert.Empty(result.CorrelationId);

foreach (var error in errors)
{
Expand Down

0 comments on commit 51f2b90

Please sign in to comment.