From ac47d309d1a20b45cf71eca420ac023f281d76e1 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 29 Aug 2023 08:50:54 +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 79bdfe4..0258907 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ This library is an ad hoc code customization used in my private/work projects th | 405 | Exception.NotAllowedException | Response.MethodNotAllowed | available | | 406 | Exception.NotAcceptableException | Response.NotAcceptable | available | | 408 | Exception.RequestTimeoutException | Response.RequestTimeout | available | -| 409 | Exception.ConflictException | Response.Conflict | coming soon | +| 409 | Exception.ConflictException | Response.Conflict | available | | 422 | Exception.UnprocessableEntityException | Response.UnprocessableEntity | coming soon | | 500 | Exception.InternalServerErrorException | Response.InternalServerError | coming soon | | 501 | Exception.NotImplementedException | Response.NotImplemented | coming soon | From 90d7a6dc8cb05090ccd1b1db0010c4b1de9f024c Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 29 Aug 2023 08:51:05 +0200 Subject: [PATCH 2/2] Implementata exception status code 409 --- .../Exception/ConflictException.cs | 16 ++++++++++++++ .../Response/Response.cs | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/ConflictException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/ConflictException.cs b/src/CustomLibrary.ProblemDetails/Exception/ConflictException.cs new file mode 100644 index 0000000..314b368 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/ConflictException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class ConflictException : System.Exception +{ + public ConflictException() + { + } + + public ConflictException(string message) : base(message) + { + } + + public ConflictException(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 5530e66..edae28d 100644 --- a/src/CustomLibrary.ProblemDetails/Response/Response.cs +++ b/src/CustomLibrary.ProblemDetails/Response/Response.cs @@ -177,4 +177,26 @@ public static ObjectResult RequestTimeout(HttpContext httpContext, System.Except return result; } + + public static ObjectResult Conflict(HttpContext httpContext, System.Exception exc) + { + var statusCode = StatusCodes.Status409Conflict; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "Conflict" + }; + + 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