Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit accdb3a

Browse files
authored
Merge pull request #30 from AngeloDotNet/3-status-code-management-503
3 status code management 503
2 parents 33847b5 + b866cc2 commit accdb3a

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
2727
| 500 | InternalServerErrorException | available |
2828
| 501 | NotImplementedException | available |
2929
| 502 | BadGatewayException | available |
30-
| 503 | ServiceUnavailableException | coming soon |
30+
| 503 | ServiceUnavailableException | available |
3131
| 504 | GatewayTimeoutException | coming soon |
3232

3333
## Contributing

src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.Net.Mime;
2-
using Microsoft.AspNetCore.Mvc;
3-
4-
namespace CustomLibrary.ProblemDetails.Sample.Controllers;
1+
namespace CustomLibrary.ProblemDetails.Sample.Controllers;
52

63
[ApiController]
74
[Route("api/[controller]")]
@@ -193,4 +190,18 @@ public async Task<IActionResult> GetExceptionBadGatewayAsync()
193190
return ResponseException.BadGateway(HttpContext, exc);
194191
}
195192
}
193+
194+
[HttpGet("ServiceUnavailable")]
195+
public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
196+
{
197+
try
198+
{
199+
await Task.Delay(500);
200+
throw new Exception.ServiceUnavailableException("Service Unavailable");
201+
}
202+
catch (Exception.ServiceUnavailableException exc)
203+
{
204+
return ResponseException.ServiceUnavailable(HttpContext, exc);
205+
}
206+
}
196207
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
global using System.Net.Mime;
2+
global using Hellang.Middleware.ProblemDetails;
3+
global using Microsoft.AspNetCore.Mvc;
4+
global using Microsoft.AspNetCore.Server.Kestrel.Core;
5+
global using Microsoft.OpenApi.Models;

src/CustomLibrary.ProblemDetails.Sample/Startup.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using Hellang.Middleware.ProblemDetails;
2-
using Microsoft.AspNetCore.Server.Kestrel.Core;
3-
using Microsoft.OpenApi.Models;
4-
5-
namespace CustomLibrary.ProblemDetails.Sample;
1+
namespace CustomLibrary.ProblemDetails.Sample;
62

73
public class Startup
84
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace CustomLibrary.ProblemDetails.Exception;
2+
3+
public class ServiceUnavailableException : System.Exception
4+
{
5+
public ServiceUnavailableException()
6+
{
7+
}
8+
9+
public ServiceUnavailableException(string message) : base(message)
10+
{
11+
}
12+
13+
public ServiceUnavailableException(string message, System.Exception innerException) : base(message, innerException)
14+
{
15+
}
16+
}

src/CustomLibrary.ProblemDetails/ResponseException.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,32 @@ public static ObjectResult BadGateway(HttpContext httpContext, System.Exception
365365

366366
return result;
367367
}
368+
369+
public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
370+
{
371+
var statusCode = StatusCodes.Status503ServiceUnavailable;
372+
var problemDetails = new CustomProblemDetails
373+
{
374+
Status = statusCode,
375+
Detail = exc.Message,
376+
Type = $"https://httpstatuses.com/{statusCode}",
377+
Instance = httpContext.Request.Path,
378+
Title = "ServiceUnavailable"
379+
};
380+
381+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
382+
//problemDetails.Extensions.Add("errors", exc.Message);
383+
384+
if (validationError?.Any() ?? false)
385+
{
386+
problemDetails.Extensions.Add("errors", validationError);
387+
}
388+
389+
var result = new ObjectResult(problemDetails)
390+
{
391+
StatusCode = statusCode
392+
};
393+
394+
return result;
395+
}
368396
}

0 commit comments

Comments
 (0)