From 6351eca9e1eb771bc78b2cb10dcc1899810f269a Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Fri, 1 Mar 2024 11:25:58 -0500 Subject: [PATCH] refactor: Create strongly typed objects to describe errors --- src/AspNetCore/Reasons/UnsupportedStatusError.cs | 10 ++++++++++ src/AspNetCore/ResultExtensions.cs | 4 ++-- src/Core/PagedInfo.cs | 12 ++++++++---- tests/AspNetCore/ToActionResultTests.cs | 2 +- tests/AspNetCore/ToHttpResultTests.cs | 2 +- tests/Core/PagedInfoTests.cs | 2 +- 6 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/AspNetCore/Reasons/UnsupportedStatusError.cs diff --git a/src/AspNetCore/Reasons/UnsupportedStatusError.cs b/src/AspNetCore/Reasons/UnsupportedStatusError.cs new file mode 100644 index 0000000..d47cc2c --- /dev/null +++ b/src/AspNetCore/Reasons/UnsupportedStatusError.cs @@ -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); +} diff --git a/src/AspNetCore/ResultExtensions.cs b/src/AspNetCore/ResultExtensions.cs index 9918efe..480f05d 100644 --- a/src/AspNetCore/ResultExtensions.cs +++ b/src/AspNetCore/ResultExtensions.cs @@ -92,7 +92,7 @@ public static ActionResult 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) }; /// @@ -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) }; } diff --git a/src/Core/PagedInfo.cs b/src/Core/PagedInfo.cs index 9290f8a..7de382d 100644 --- a/src/Core/PagedInfo.cs +++ b/src/Core/PagedInfo.cs @@ -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); + } } diff --git a/tests/AspNetCore/ToActionResultTests.cs b/tests/AspNetCore/ToActionResultTests.cs index 23e0249..dd512da 100644 --- a/tests/AspNetCore/ToActionResultTests.cs +++ b/tests/AspNetCore/ToActionResultTests.cs @@ -142,7 +142,7 @@ public void ToActionResult_WhenOperationResultIsNotSupported_ShouldThrowNotSuppo { // Arrange var result = new Result { Status = (ResultStatus)5000 }; - var expectedMessage = string.Format(ResponseMessages.UnsupportedStatus, result.Status); + var expectedMessage = new UnsupportedStatusError(result.Status).Message; // Act Action act = () => result.ToActionResult(); diff --git a/tests/AspNetCore/ToHttpResultTests.cs b/tests/AspNetCore/ToHttpResultTests.cs index 5a2a4e7..9125661 100644 --- a/tests/AspNetCore/ToHttpResultTests.cs +++ b/tests/AspNetCore/ToHttpResultTests.cs @@ -142,7 +142,7 @@ public void ToHttpResult_WhenOperationResultIsNotSupported_ShouldThrowNotSupport { // Arrange var result = new Result { Status = (ResultStatus)5000 }; - var expectedMessage = string.Format(ResponseMessages.UnsupportedStatus, result.Status); + var expectedMessage = new UnsupportedStatusError(result.Status).Message; // Act Action act = () => result.ToHttpResult(); diff --git a/tests/Core/PagedInfoTests.cs b/tests/Core/PagedInfoTests.cs index b2c3416..9a471eb 100644 --- a/tests/Core/PagedInfoTests.cs +++ b/tests/Core/PagedInfoTests.cs @@ -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 = () =>