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 @@ -27,7 +27,7 @@ A full example is available in the CustomLibrary.ProblemDetails.Sample folder or
| 500 | InternalServerErrorException | available |
| 501 | NotImplementedException | available |
| 502 | BadGatewayException | available |
| 503 | ServiceUnavailableException | coming soon |
| 503 | ServiceUnavailableException | available |
| 504 | GatewayTimeoutException | coming soon |

## Contributing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;

namespace CustomLibrary.ProblemDetails.Sample.Controllers;
namespace CustomLibrary.ProblemDetails.Sample.Controllers;

[ApiController]
[Route("api/[controller]")]
Expand Down Expand Up @@ -193,4 +190,18 @@ public async Task<IActionResult> GetExceptionBadGatewayAsync()
return ResponseException.BadGateway(HttpContext, exc);
}
}

[HttpGet("ServiceUnavailable")]
public async Task<IActionResult> GetExceptionServiceUnavailableAsync()
{
try
{
await Task.Delay(500);
throw new Exception.ServiceUnavailableException("Service Unavailable");
}
catch (Exception.ServiceUnavailableException exc)
{
return ResponseException.ServiceUnavailable(HttpContext, exc);
}
}
}
5 changes: 5 additions & 0 deletions src/CustomLibrary.ProblemDetails.Sample/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using System.Net.Mime;
global using Hellang.Middleware.ProblemDetails;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Server.Kestrel.Core;
global using Microsoft.OpenApi.Models;
6 changes: 1 addition & 5 deletions src/CustomLibrary.ProblemDetails.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.OpenApi.Models;

namespace CustomLibrary.ProblemDetails.Sample;
namespace CustomLibrary.ProblemDetails.Sample;

public class Startup
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CustomLibrary.ProblemDetails.Exception;

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

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

public ServiceUnavailableException(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 @@ -365,4 +365,32 @@ public static ObjectResult BadGateway(HttpContext httpContext, System.Exception

return result;
}

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

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