From 3d895a54fbd346072f1b22bc3bd34a5437cb176c Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 24 Oct 2023 09:50:11 +0200 Subject: [PATCH 1/3] Aggiornato README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba723e4..d67f50e 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or | 409 | ConflictException | available | | 422 | UnprocessableEntityException | available | | 500 | InternalServerErrorException | available | -| 501 | NotImplementedException | coming soon | +| 501 | NotImplementedException | available | | 502 | BadGatewayException | coming soon | | 503 | ServiceUnavailableException | coming soon | | 504 | GatewayTimeoutException | coming soon | From eb5fc1a8a9b64d23b8f696d9afb9da753b37cc8a Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 24 Oct 2023 09:51:26 +0200 Subject: [PATCH 2/3] Implementata exception status code 501 - close #5 --- .../Exception/NotImplementedException.cs | 16 +++++++++++ .../ResponseException.cs | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/NotImplementedException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/NotImplementedException.cs b/src/CustomLibrary.ProblemDetails/Exception/NotImplementedException.cs new file mode 100644 index 0000000..1dcda28 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/NotImplementedException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class NotImplementedException : System.Exception +{ + public NotImplementedException() + { + } + + public NotImplementedException(string message) : base(message) + { + } + + public NotImplementedException(string message, System.Exception innerException) : base(message, innerException) + { + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails/ResponseException.cs b/src/CustomLibrary.ProblemDetails/ResponseException.cs index fef63ed..a2cf791 100644 --- a/src/CustomLibrary.ProblemDetails/ResponseException.cs +++ b/src/CustomLibrary.ProblemDetails/ResponseException.cs @@ -309,4 +309,32 @@ public static ObjectResult InternalServerError(HttpContext httpContext, System.E return result; } + + public static ObjectResult NotImplemented(HttpContext httpContext, System.Exception exc, List validationError = null) + { + var statusCode = StatusCodes.Status501NotImplemented; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Detail = exc.Message, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "InternalServerError" + }; + + problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier); + //problemDetails.Extensions.Add("errors", exc.Message); + + if (validationError?.Any() ?? false) + { + problemDetails.Extensions.Add("errors", validationError); + } + + var result = new ObjectResult(problemDetails) + { + StatusCode = statusCode + }; + + return result; + } } \ No newline at end of file From b0fa7426427e210eb20af22cc759c1896145eecb Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 24 Oct 2023 09:52:42 +0200 Subject: [PATCH 3/3] Aggiunto esempio StatusCode 501 - NotImplemented --- .../Controllers/TestController.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs index 49ecbba..8e7543f 100644 --- a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs +++ b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs @@ -165,4 +165,18 @@ public async Task GetExceptionInternalServerErrorAsync() return ResponseException.InternalServerError(HttpContext, exc); } } + + [HttpGet("NotImplemented")] + public async Task GetExceptionNotImplementedAsync() + { + try + { + await Task.Delay(500); + throw new Exception.NotImplementedException("Not Implemented"); + } + catch (Exception.NotImplementedException exc) + { + return ResponseException.NotImplemented(HttpContext, exc); + } + } } \ No newline at end of file