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 | 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 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