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

2 status code management 504 #31

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
| 501 | NotImplementedException | available |
| 502 | BadGatewayException | available |
| 503 | ServiceUnavailableException | available |
| 504 | GatewayTimeoutException | coming soon |
| 504 | GatewayTimeoutException | available |

## Contributing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
}
}

[HttpGet("NotImplemented")]
[HttpGet("NotImplemented-Exception")]
public async Task<IActionResult> GetExceptionNotImplementedAsync()
{
try
Expand All @@ -177,7 +177,7 @@ public async Task<IActionResult> GetExceptionNotImplementedAsync()
}
}

[HttpGet("BadGateway")]
[HttpGet("BadGateway-Exception")]
public async Task<IActionResult> GetExceptionBadGatewayAsync()
{
try
Expand All @@ -191,7 +191,7 @@ public async Task<IActionResult> GetExceptionBadGatewayAsync()
}
}

[HttpGet("ServiceUnavailable")]
[HttpGet("ServiceUnavailable-Exception")]
public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
{
try
Expand All @@ -204,4 +204,18 @@ public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
return ResponseException.ServiceUnavailable(HttpContext, exc);
}
}

[HttpGet("GatewayTimeout-Exception")]
public async Task<IActionResult> GetExceptionGatewayTimeoutAsync()
{
try
{
await Task.Delay(500);
throw new Exception.GatewayTimeoutException("Gateway Timeout");
}
catch (Exception.GatewayTimeoutException exc)
{
return ResponseException.GatewayTimeout(HttpContext, exc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CustomLibrary.ProblemDetails.Exception;

public class GatewayTimeoutException : System.Exception
{
public GatewayTimeoutException()
{
}

public GatewayTimeoutException(string message) : base(message)
{
}

public GatewayTimeoutException(string message, System.Exception innerException) : base(message, innerException)
{
}
}
28 changes: 28 additions & 0 deletions src/CustomLibrary.ProblemDetails/ResponseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,32 @@ public static ObjectResult ServiceUnavailable(HttpContext httpContext, System.Ex

return result;
}

public static ObjectResult GatewayTimeout(HttpContext httpContext, System.Exception exc, List<string> validationError = null)
{
var statusCode = StatusCodes.Status504GatewayTimeout;
var problemDetails = new CustomProblemDetails
{
Status = statusCode,
Detail = exc.Message,
Type = $"https://httpstatuses.com/{statusCode}",
Instance = httpContext.Request.Path,
Title = "GatewayTimeout"
};

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