Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
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 @@ -26,7 +26,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
| 422 | UnprocessableEntityException | available |
| 500 | InternalServerErrorException | available |
| 501 | NotImplementedException | available |
| 502 | BadGatewayException | coming soon |
| 502 | BadGatewayException | available |
| 503 | ServiceUnavailableException | coming soon |
| 504 | GatewayTimeoutException | coming soon |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,18 @@ public async Task<IActionResult> GetExceptionNotImplementedAsync()
return ResponseException.NotImplemented(HttpContext, exc);
}
}

[HttpGet("BadGateway")]
public async Task<IActionResult> GetExceptionBadGatewayAsync()
{
try
{
await Task.Delay(500);
throw new Exception.BadGatewayException("Bad Gateway");
}
catch (Exception.BadGatewayException exc)
{
return ResponseException.BadGateway(HttpContext, exc);
}
}
}
15 changes: 15 additions & 0 deletions src/CustomLibrary.ProblemDetails/Exception/BadGatewayException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace CustomLibrary.ProblemDetails.Exception;
public class BadGatewayException : System.Exception
{
public BadGatewayException()
{
}

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

public BadGatewayException(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 @@ -337,4 +337,32 @@ public static ObjectResult NotImplemented(HttpContext httpContext, System.Except

return result;
}

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

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