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

Commit 33847b5

Browse files
authored
Merge pull request #29 from AngeloDotNet/4-status-code-management-502
4 status code management 502
2 parents 6ffb8a2 + 876c658 commit 33847b5

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,18 @@ public async Task<IActionResult> GetExceptionNotImplementedAsync()
179179
return ResponseException.NotImplemented(HttpContext, exc);
180180
}
181181
}
182+
183+
[HttpGet("BadGateway")]
184+
public async Task<IActionResult> GetExceptionBadGatewayAsync()
185+
{
186+
try
187+
{
188+
await Task.Delay(500);
189+
throw new Exception.BadGatewayException("Bad Gateway");
190+
}
191+
catch (Exception.BadGatewayException exc)
192+
{
193+
return ResponseException.BadGateway(HttpContext, exc);
194+
}
195+
}
182196
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace CustomLibrary.ProblemDetails.Exception;
2+
public class BadGatewayException : System.Exception
3+
{
4+
public BadGatewayException()
5+
{
6+
}
7+
8+
public BadGatewayException(string message) : base(message)
9+
{
10+
}
11+
12+
public BadGatewayException(string message, System.Exception innerException) : base(message, innerException)
13+
{
14+
}
15+
}

src/CustomLibrary.ProblemDetails/ResponseException.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,32 @@ public static ObjectResult NotImplemented(HttpContext httpContext, System.Except
337337

338338
return result;
339339
}
340+
341+
public static ObjectResult BadGateway(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
342+
{
343+
var statusCode = StatusCodes.Status502BadGateway;
344+
var problemDetails = new CustomProblemDetails
345+
{
346+
Status = statusCode,
347+
Detail = exc.Message,
348+
Type = $"https://httpstatuses.com/{statusCode}",
349+
Instance = httpContext.Request.Path,
350+
Title = "BadGateway"
351+
};
352+
353+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
354+
//problemDetails.Extensions.Add("errors", exc.Message);
355+
356+
if (validationError?.Any() ?? false)
357+
{
358+
problemDetails.Extensions.Add("errors", validationError);
359+
}
360+
361+
var result = new ObjectResult(problemDetails)
362+
{
363+
StatusCode = statusCode
364+
};
365+
366+
return result;
367+
}
340368
}

0 commit comments

Comments
 (0)