32 lines
1.0 KiB
C#
32 lines
1.0 KiB
C#
using Hl7.Fhir.Serialization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
public class FhirExceptionFilter : IExceptionFilter
|
|
{
|
|
private static readonly FhirJsonSerializer Serializer = new();
|
|
|
|
public void OnException(ExceptionContext context)
|
|
{
|
|
if (!context.HttpContext.Request.Path.StartsWithSegments("/fhir"))
|
|
return;
|
|
|
|
var outcome = FhirOperationOutcomeBuilder.FromException(context.Exception);
|
|
var status = context.Exception switch
|
|
{
|
|
FhirMappingException fme => fme.HttpStatus,
|
|
NotFoundException => 404,
|
|
ValidationException => 422,
|
|
ConflictException => 409,
|
|
_ => 500
|
|
};
|
|
|
|
context.Result = new ContentResult
|
|
{
|
|
StatusCode = status,
|
|
ContentType = "application/fhir+json",
|
|
Content = Serializer.SerializeToString(outcome)
|
|
};
|
|
context.ExceptionHandled = true;
|
|
}
|
|
} |