From 7d51b766f732a20346809a20f5b42949b7bca27d Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Sun, 27 Aug 2023 15:50:28 +0200 Subject: [PATCH] Implementata exception status code 408 close #15 --- .../Exception/RequestTimeoutException.cs | 16 ++++++++++++++ .../Response/Response.cs | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails/Exception/RequestTimeoutException.cs diff --git a/src/CustomLibrary.ProblemDetails/Exception/RequestTimeoutException.cs b/src/CustomLibrary.ProblemDetails/Exception/RequestTimeoutException.cs new file mode 100644 index 0000000..b8ce969 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/RequestTimeoutException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class RequestTimeoutException : System.Exception +{ + public RequestTimeoutException() + { + } + + public RequestTimeoutException(string message) : base(message) + { + } + + public RequestTimeoutException(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 90e3cd0..5530e66 100644 --- a/src/CustomLibrary.ProblemDetails/Response/Response.cs +++ b/src/CustomLibrary.ProblemDetails/Response/Response.cs @@ -155,4 +155,26 @@ public static ObjectResult NotAcceptable(HttpContext httpContext, System.Excepti return result; } + + public static ObjectResult RequestTimeout(HttpContext httpContext, System.Exception exc) + { + var statusCode = StatusCodes.Status408RequestTimeout; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "RequestTimeout" + }; + + 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