From ba68a6a01a1ffa444795dbd8485931f6c1c78367 Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 31 Oct 2023 21:09:27 +0100 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 d317806..e1a9e67 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or | 501 | NotImplementedException | available | | 502 | BadGatewayException | available | | 503 | ServiceUnavailableException | available | -| 504 | GatewayTimeoutException | coming soon | +| 504 | GatewayTimeoutException | available | ## Contributing From b04f5b92544a743099d3058ed3e4e06db9a807ee Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 31 Oct 2023 21:09:51 +0100 Subject: [PATCH 2/3] Implementata exception status code 504 - close #2 --- .../Exception/GatewayTimeoutException.cs | 16 +++++++++++ .../ResponseException.cs | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/GatewayTimeoutException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/GatewayTimeoutException.cs b/src/CustomLibrary.ProblemDetails/Exception/GatewayTimeoutException.cs new file mode 100644 index 0000000..4c8400b --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/GatewayTimeoutException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class GatewayTimeoutException : System.Exception +{ + public GatewayTimeoutException() + { + } + + public GatewayTimeoutException(string message) : base(message) + { + } + + public GatewayTimeoutException(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 7e50ab5..1bea4a7 100644 --- a/src/CustomLibrary.ProblemDetails/ResponseException.cs +++ b/src/CustomLibrary.ProblemDetails/ResponseException.cs @@ -393,4 +393,32 @@ public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Ex return result; } + + public static ObjectResult GatewayTimeout(HttpContext httpContext, System.Exception exc, List validationError = null) + { + var statusCode = StatusCodes.Status504GatewayTimeout; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Detail = exc.Message, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "GatewayTimeout" + }; + + 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 534001c6e6adeef1a18f999c9084e1df9f04dcde Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 31 Oct 2023 21:10:34 +0100 Subject: [PATCH 3/3] Aggiunto esempio StatusCode 504 - GatewayTimeout --- .../Controllers/TestController.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs index 753723b..f742872 100644 --- a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs +++ b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs @@ -163,7 +163,7 @@ public async Task GetExceptionInternalServerErrorAsync() } } - [HttpGet("NotImplemented")] + [HttpGet("NotImplemented-Exception")] public async Task GetExceptionNotImplementedAsync() { try @@ -177,7 +177,7 @@ public async Task GetExceptionNotImplementedAsync() } } - [HttpGet("BadGateway")] + [HttpGet("BadGateway-Exception")] public async Task GetExceptionBadGatewayAsync() { try @@ -191,7 +191,7 @@ public async Task GetExceptionBadGatewayAsync() } } - [HttpGet("ServiceUnavailable")] + [HttpGet("ServiceUnavailable-Exception")] public async Task GetExceptionServiceUnavailableAsync() { try @@ -204,4 +204,18 @@ public async Task GetExceptionServiceUnavailableAsync() return ResponseException.ServiceUnavailable(HttpContext, exc); } } + + [HttpGet("GatewayTimeout-Exception")] + public async Task GetExceptionGatewayTimeoutAsync() + { + try + { + await Task.Delay(500); + throw new Exception.GatewayTimeoutException("Gateway Timeout"); + } + catch (Exception.GatewayTimeoutException exc) + { + return ResponseException.GatewayTimeout(HttpContext, exc); + } + } } \ No newline at end of file