From 8952ea208172d8f3c4a3ed6a66563ca89aa5038a Mon Sep 17 00:00:00 2001 From: JosnocSackra Date: Thu, 15 Jun 2023 19:50:30 -0600 Subject: [PATCH] Added CriticalError to ResultStatuses (#140) --- .../ResultStatusMap.cs | 20 +++++++++++++++++-- src/Ardalis.Result/Result.cs | 12 +++++++++++ src/Ardalis.Result/ResultExtensions.cs | 1 + src/Ardalis.Result/ResultStatus.cs | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Ardalis.Result.AspNetCore/ResultStatusMap.cs b/src/Ardalis.Result.AspNetCore/ResultStatusMap.cs index 9cb29cd..d86e1ae 100644 --- a/src/Ardalis.Result.AspNetCore/ResultStatusMap.cs +++ b/src/Ardalis.Result.AspNetCore/ResultStatusMap.cs @@ -35,7 +35,10 @@ public ResultStatusMap AddDefaultMap() .For(ResultStatus.NotFound, HttpStatusCode.NotFound, resultStatusOptions => resultStatusOptions .With(NotFoundEntity)) .For(ResultStatus.Conflict, HttpStatusCode.Conflict, resultStatusOptions => resultStatusOptions - .With(ConflictEntity)); + .With(ConflictEntity)) + .For(ResultStatus.CriticalError, HttpStatusCode.InternalServerError, resultStatusOptions => + resultStatusOptions + .With(CriticalEntity)); } /// @@ -139,6 +142,19 @@ private static ProblemDetails ConflictEntity(ControllerBase controller, IResult Detail = result.Errors.Any() ? details.ToString() : null }; } + + private static ProblemDetails CriticalEntity(ControllerBase controller, IResult result) + { + var details = new StringBuilder("Next error(s) occured:"); + + foreach (var error in result.Errors) details.Append("* ").Append(error).AppendLine(); + + return new ProblemDetails + { + Title = "Something went wrong.", + Detail = result.Errors.Any() ? details.ToString() : null + }; + } } public class ResultStatusOptions @@ -217,4 +233,4 @@ public ResultStatusOptions With(Type responseType, Func Conflict(params string[] errorMessages) { return new Result(ResultStatus.Conflict) { Errors = errorMessages }; } + + /// + /// Represents a critical error that occurred during the execution of the service. + /// Everything provided by the user was valid, but the service was unable to complete due to an exception. + /// See also HTTP 500 Internal Server Error: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors + /// + /// A list of string error messages. + /// A Result + public static Result CriticalError(params string[] errorMessages) + { + return new Result(ResultStatus.CriticalError) { Errors = errorMessages }; + } } } diff --git a/src/Ardalis.Result/ResultExtensions.cs b/src/Ardalis.Result/ResultExtensions.cs index 4a4bba1..4643ce1 100644 --- a/src/Ardalis.Result/ResultExtensions.cs +++ b/src/Ardalis.Result/ResultExtensions.cs @@ -29,6 +29,7 @@ public static Result Map(this Result.Conflict(result.Errors.ToArray()) : Result.Conflict(); + case ResultStatus.CriticalError: return Result.CriticalError(result.Errors.ToArray()); default: throw new NotSupportedException($"Result {result.Status} conversion is not supported."); } diff --git a/src/Ardalis.Result/ResultStatus.cs b/src/Ardalis.Result/ResultStatus.cs index de2e3fc..57e2750 100644 --- a/src/Ardalis.Result/ResultStatus.cs +++ b/src/Ardalis.Result/ResultStatus.cs @@ -9,5 +9,6 @@ public enum ResultStatus Invalid, NotFound, Conflict, + CriticalError } }