Skip to content

Setup ExceptionHandling

Harald Sømnes Hanssen edited this page Jun 3, 2019 · 4 revisions

See https://github.com/verzada/FhirStarter.DotNetCore/blob/master/src/FhirStarter.R4.Twisted.Core/Startup.cs

Easiest way to the goal is to add the following code and possibly modify it.

        app.UseExceptionHandler(a => a.Run(async context =>
        {
            var feature = context.Features.Get<IExceptionHandlerPathFeature>();
            if (feature?.Error != null)
            {
                var exception = feature.Error;

                var operationOutcome = ErrorHandlingMiddleware.GetOperationOutCome(exception, true);
                var fhirJsonConverter = new FhirJsonSerializer();
                var result = fhirJsonConverter.SerializeToString(operationOutcome);
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(result);
            }
        })); 

Clone this wiki locally