diff --git a/README.md b/README.md index 596185d..d317806 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or | 500 | InternalServerErrorException | available | | 501 | NotImplementedException | available | | 502 | BadGatewayException | available | -| 503 | ServiceUnavailableException | coming soon | +| 503 | ServiceUnavailableException | available | | 504 | GatewayTimeoutException | coming soon | ## Contributing diff --git a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs index 368f148..753723b 100644 --- a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs +++ b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs @@ -1,7 +1,4 @@ -using System.Net.Mime; -using Microsoft.AspNetCore.Mvc; - -namespace CustomLibrary.ProblemDetails.Sample.Controllers; +namespace CustomLibrary.ProblemDetails.Sample.Controllers; [ApiController] [Route("api/[controller]")] @@ -193,4 +190,18 @@ public async Task GetExceptionBadGatewayAsync() return ResponseException.BadGateway(HttpContext, exc); } } + + [HttpGet("ServiceUnavailable")] + public async Task GetExceptionServiceUnavailableAsync() + { + try + { + await Task.Delay(500); + throw new Exception.ServiceUnavailableException("Service Unavailable"); + } + catch (Exception.ServiceUnavailableException exc) + { + return ResponseException.ServiceUnavailable(HttpContext, exc); + } + } } \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails.Sample/GlobalUsings.cs b/src/CustomLibrary.ProblemDetails.Sample/GlobalUsings.cs new file mode 100644 index 0000000..4159af5 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/GlobalUsings.cs @@ -0,0 +1,5 @@ +global using System.Net.Mime; +global using Hellang.Middleware.ProblemDetails; +global using Microsoft.AspNetCore.Mvc; +global using Microsoft.AspNetCore.Server.Kestrel.Core; +global using Microsoft.OpenApi.Models; \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails.Sample/Startup.cs b/src/CustomLibrary.ProblemDetails.Sample/Startup.cs index d703dc5..060bb57 100644 --- a/src/CustomLibrary.ProblemDetails.Sample/Startup.cs +++ b/src/CustomLibrary.ProblemDetails.Sample/Startup.cs @@ -1,8 +1,4 @@ -using Hellang.Middleware.ProblemDetails; -using Microsoft.AspNetCore.Server.Kestrel.Core; -using Microsoft.OpenApi.Models; - -namespace CustomLibrary.ProblemDetails.Sample; +namespace CustomLibrary.ProblemDetails.Sample; public class Startup { diff --git a/src/CustomLibrary.ProblemDetails/Exception/ServiceUnavailableException.cs b/src/CustomLibrary.ProblemDetails/Exception/ServiceUnavailableException.cs new file mode 100644 index 0000000..ffa54a7 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails/Exception/ServiceUnavailableException.cs @@ -0,0 +1,16 @@ +namespace CustomLibrary.ProblemDetails.Exception; + +public class ServiceUnavailableException : System.Exception +{ + public ServiceUnavailableException() + { + } + + public ServiceUnavailableException(string message) : base(message) + { + } + + public ServiceUnavailableException(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 bf47316..7e50ab5 100644 --- a/src/CustomLibrary.ProblemDetails/ResponseException.cs +++ b/src/CustomLibrary.ProblemDetails/ResponseException.cs @@ -365,4 +365,32 @@ public static ObjectResult BadGateway(HttpContext httpContext, System.Exception return result; } + + public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Exception exc, List validationError = null) + { + var statusCode = StatusCodes.Status503ServiceUnavailable; + var problemDetails = new CustomProblemDetails + { + Status = statusCode, + Detail = exc.Message, + Type = $"https://httpstatuses.com/{statusCode}", + Instance = httpContext.Request.Path, + Title = "ServiceUnavailable" + }; + + 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