38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using System.Text.Json;
|
|
using FinalFactory.Rendezvous.Contracts;
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
|
|
namespace FinalFactory.Rendezvous.Server.Http;
|
|
|
|
internal sealed class RendezvousExceptionHandler : IExceptionHandler
|
|
{
|
|
public async ValueTask<bool> TryHandleAsync(
|
|
HttpContext httpContext,
|
|
Exception exception,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (httpContext.Response.HasStarted)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool invalidRequest = exception is BadHttpRequestException or JsonException;
|
|
httpContext.Response.StatusCode = invalidRequest
|
|
? StatusCodes.Status400BadRequest
|
|
: StatusCodes.Status500InternalServerError;
|
|
await httpContext.Response.WriteAsJsonAsync(
|
|
new ApiError
|
|
{
|
|
Code = invalidRequest
|
|
? RendezvousErrorCode.InvalidRequest
|
|
: RendezvousErrorCode.InternalError,
|
|
Message = invalidRequest
|
|
? "The request body, route, or query value is invalid."
|
|
: "The service could not complete the request.",
|
|
},
|
|
ContractJson.Options,
|
|
cancellationToken).ConfigureAwait(false);
|
|
return true;
|
|
}
|
|
}
|