From d174cc55af605d8d37fb83797600bbf928c50ef7 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Sun, 29 Oct 2023 00:20:13 +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 d67f50e..596185d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or | 422 | UnprocessableEntityException | available | | 500 | InternalServerErrorException | available | | 501 | NotImplementedException | available | -| 502 | BadGatewayException | coming soon | +| 502 | BadGatewayException | available | | 503 | ServiceUnavailableException | coming soon | | 504 | GatewayTimeoutException | coming soon | From c83c3fc9acf9b56d4a232b8cfaea9eb3224968e1 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Sun, 29 Oct 2023 00:20:38 +0200 Subject: [PATCH 2/3] Implementata exception status code 502 - close #4 --- .../Exception/BadGatewayException.cs | 15 ++++++++++ .../ResponseException.cs | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/BadGatewayException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/BadGatewayException.cs b/src/CustomLibrary.ProblemDetails/Exception/BadGatewayException.cs new file mode 100644 index 0000000..34607c8 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/BadGatewayException.cs @@ -0,0 +1,15 @@ +namespace CustomLibrary.ProblemDetails.Exception; +public class BadGatewayException : System.Exception +{ + public BadGatewayException() + { + } + + public BadGatewayException(string message) : base(message) + { + } + + public BadGatewayException(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 a2cf791..bf47316 100644 --- a/src/CustomLibrary.ProblemDetails/ResponseException.cs +++ b/src/CustomLibrary.ProblemDetails/ResponseException.cs @@ -337,4 +337,32 @@ public static ObjectResult NotImplemented(HttpContext httpContext, System.Except return result; } + + public static ObjectResult BadGateway(HttpContext httpContext, System.Exception exc, List validationError = null) + { + var statusCode = StatusCodes.Status502BadGateway; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Detail = exc.Message, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "BadGateway" + }; + + 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 876c65823fe7aa3868d068b93ef623b3c3ae3f04 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Sun, 29 Oct 2023 00:20:57 +0200 Subject: [PATCH 3/3] Aggiunto esempio StatusCode 502 - Bad Gateway --- .../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 8e7543f..368f148 100644 --- a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs +++ b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs @@ -179,4 +179,18 @@ public async Task GetExceptionNotImplementedAsync() return ResponseException.NotImplemented(HttpContext, exc); } } + + [HttpGet("BadGateway")] + public async Task GetExceptionBadGatewayAsync() + { + try + { + await Task.Delay(500); + throw new Exception.BadGatewayException("Bad Gateway"); + } + catch (Exception.BadGatewayException exc) + { + return ResponseException.BadGateway(HttpContext, exc); + } + } } \ No newline at end of file