From 586a346ba57749c7dfaabf926e08b862587c37f2 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 12 Sep 2023 19:38:46 +0200 Subject: [PATCH 1/2] Aggiornato README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0258907..661c61b 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ This library is an ad hoc code customization used in my private/work projects th | 406 | Exception.NotAcceptableException | Response.NotAcceptable | available | | 408 | Exception.RequestTimeoutException | Response.RequestTimeout | available | | 409 | Exception.ConflictException | Response.Conflict | available | -| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | coming soon | +| 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | available | | 500 | Exception.InternalServerErrorException | Response.InternalServerError | coming soon | | 501 | Exception.NotImplementedException | Response.NotImplemented | coming soon | | 502 | Exception.BadGatewayException | Response.BadGateway | coming soon | From 098aef062d9f75da6b5f95d1793534df5401d9f2 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 12 Sep 2023 19:39:22 +0200 Subject: [PATCH 2/2] Implementata exception status code 422 - close #6 --- .../Exception/UnprocessableEntityException.cs | 16 ++++++++++++++ .../Response/Response.cs | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs b/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs new file mode 100644 index 0000000..1262168 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/UnprocessableEntityException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class UnprocessableEntityException : System.Exception +{ + public UnprocessableEntityException() + { + } + + public UnprocessableEntityException(string message) : base(message) + { + } + + public UnprocessableEntityException(string message, System.Exception innerException) : base(message, innerException) + { + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails/Response/Response.cs b/src/CustomLibrary.ProblemDetails/Response/Response.cs index edae28d..3af2ad7 100644 --- a/src/CustomLibrary.ProblemDetails/Response/Response.cs +++ b/src/CustomLibrary.ProblemDetails/Response/Response.cs @@ -199,4 +199,26 @@ public static ObjectResult Conflict(HttpContext httpContext, System.Exception ex return result; } + + public static ObjectResult UnprocessableEntity(HttpContext httpContext, System.Exception exc) + { + var statusCode = StatusCodes.Status422UnprocessableEntity; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "UnprocessableEntity" + }; + + problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier); + problemDetails.Extensions.Add("errors", exc.Message); + + var result = new ObjectResult(problemDetails) + { + StatusCode = statusCode + }; + + return result; + } } \ No newline at end of file