Skip to content

Commit

Permalink
refactor: Create strongly typed objects to describe errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Mar 1, 2024
1 parent 2882302 commit 6351eca
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/AspNetCore/Reasons/UnsupportedStatusError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SimpleResults.Resources;

namespace SimpleResults;

internal readonly ref struct UnsupportedStatusError
{
public string Message { get; }
public UnsupportedStatusError(ResultStatus status)
=> Message = string.Format(ResponseMessages.UnsupportedStatus, status);
}
4 changes: 2 additions & 2 deletions src/AspNetCore/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static ActionResult<Result> ToActionResult(this Result result)
ResultStatus.Forbidden => new ForbiddenResult(result),
ResultStatus.ByteArrayFile => FileResultConverter.ConvertToFileContentResult(result),
ResultStatus.StreamFile => FileResultConverter.ConvertToFileStreamResult(result),
_ => throw new NotSupportedException(string.Format(ResponseMessages.UnsupportedStatus, result.Status))
_ => throw new NotSupportedException(new UnsupportedStatusError(result.Status).Message)
};

/// <summary>
Expand Down Expand Up @@ -155,6 +155,6 @@ public static IResult ToHttpResult(this Result result)
ResultStatus.Forbidden => new ForbiddenHttpResult(result),
ResultStatus.ByteArrayFile => FileResultConverter.ConvertToFileContentHttpResult(result),
ResultStatus.StreamFile => FileResultConverter.ConvertToFileStreamHttpResult(result),
_ => throw new NotSupportedException(string.Format(ResponseMessages.UnsupportedStatus, result.Status))
_ => throw new NotSupportedException(new UnsupportedStatusError(result.Status).Message)
};
}
12 changes: 8 additions & 4 deletions src/Core/PagedInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ public class PagedInfo
public PagedInfo(int pageNumber, int pageSize, int totalRecords)
{
if (pageSize == 0)
{
var message = string.Format(ResponseMessages.DivideByZero, nameof(pageSize));
throw new DivideByZeroException(message);
}
throw new DivideByZeroException(new DivideByZeroError(nameof(pageSize)).Message);

PageNumber = pageNumber;
PageSize = pageSize;
TotalRecords = totalRecords;
TotalPages = (int)Math.Ceiling(totalRecords / (double)pageSize);
}

internal readonly ref struct DivideByZeroError
{
public string Message { get; }
public DivideByZeroError(string parameterName)
=> Message = string.Format(ResponseMessages.DivideByZero, parameterName);
}
}
2 changes: 1 addition & 1 deletion tests/AspNetCore/ToActionResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void ToActionResult_WhenOperationResultIsNotSupported_ShouldThrowNotSuppo
{
// Arrange
var result = new Result<Person> { Status = (ResultStatus)5000 };
var expectedMessage = string.Format(ResponseMessages.UnsupportedStatus, result.Status);
var expectedMessage = new UnsupportedStatusError(result.Status).Message;

// Act
Action act = () => result.ToActionResult();
Expand Down
2 changes: 1 addition & 1 deletion tests/AspNetCore/ToHttpResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void ToHttpResult_WhenOperationResultIsNotSupported_ShouldThrowNotSupport
{
// Arrange
var result = new Result<Person> { Status = (ResultStatus)5000 };
var expectedMessage = string.Format(ResponseMessages.UnsupportedStatus, result.Status);
var expectedMessage = new UnsupportedStatusError(result.Status).Message;

// Act
Action act = () => result.ToHttpResult();
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/PagedInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Constructor_WhenPageSizeIsZero_ShouldThrowDivideByZeroException()
{
// Arrange
int pageSize = 0;
var expectedMessage = string.Format(ResponseMessages.DivideByZero, nameof(pageSize));
var expectedMessage = new PagedInfo.DivideByZeroError(nameof(pageSize)).Message;

// Act
Action act = () =>
Expand Down

0 comments on commit 6351eca

Please sign in to comment.