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

Commit 405c920

Browse files
authored
Merge pull request #31 from AngeloDotNet/2-status-code-management-504
2 status code management 504
2 parents accdb3a + 534001c commit 405c920

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

README.md

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

3333
## Contributing
3434

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
163163
}
164164
}
165165

166-
[HttpGet("NotImplemented")]
166+
[HttpGet("NotImplemented-Exception")]
167167
public async Task<IActionResult> GetExceptionNotImplementedAsync()
168168
{
169169
try
@@ -177,7 +177,7 @@ public async Task<IActionResult> GetExceptionNotImplementedAsync()
177177
}
178178
}
179179

180-
[HttpGet("BadGateway")]
180+
[HttpGet("BadGateway-Exception")]
181181
public async Task<IActionResult> GetExceptionBadGatewayAsync()
182182
{
183183
try
@@ -191,7 +191,7 @@ public async Task<IActionResult> GetExceptionBadGatewayAsync()
191191
}
192192
}
193193

194-
[HttpGet("ServiceUnavailable")]
194+
[HttpGet("ServiceUnavailable-Exception")]
195195
public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
196196
{
197197
try
@@ -204,4 +204,18 @@ public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
204204
return ResponseException.ServiceUnavailable(HttpContext, exc);
205205
}
206206
}
207+
208+
[HttpGet("GatewayTimeout-Exception")]
209+
public async Task<IActionResult> GetExceptionGatewayTimeoutAsync()
210+
{
211+
try
212+
{
213+
await Task.Delay(500);
214+
throw new Exception.GatewayTimeoutException("Gateway Timeout");
215+
}
216+
catch (Exception.GatewayTimeoutException exc)
217+
{
218+
return ResponseException.GatewayTimeout(HttpContext, exc);
219+
}
220+
}
207221
}
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 GatewayTimeoutException : System.Exception
4+
{
5+
public GatewayTimeoutException()
6+
{
7+
}
8+
9+
public GatewayTimeoutException(string message) : base(message)
10+
{
11+
}
12+
13+
public GatewayTimeoutException(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
@@ -393,4 +393,32 @@ public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Ex
393393

394394
return result;
395395
}
396+
397+
public static ObjectResult GatewayTimeout(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
398+
{
399+
var statusCode = StatusCodes.Status504GatewayTimeout;
400+
var problemDetails = new CustomProblemDetails
401+
{
402+
Status = statusCode,
403+
Detail = exc.Message,
404+
Type = $"https://httpstatuses.com/{statusCode}",
405+
Instance = httpContext.Request.Path,
406+
Title = "GatewayTimeout"
407+
};
408+
409+
problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? httpContext.TraceIdentifier);
410+
//problemDetails.Extensions.Add("errors", exc.Message);
411+
412+
if (validationError?.Any() ?? false)
413+
{
414+
problemDetails.Extensions.Add("errors", validationError);
415+
}
416+
417+
var result = new ObjectResult(problemDetails)
418+
{
419+
StatusCode = statusCode
420+
};
421+
422+
return result;
423+
}
396424
}

0 commit comments

Comments
 (0)