private Task HandleExceptionAsync(HttpContext context, Exception exec, ILogger <ErrorHandlingMiddleware> _logger) { _logger.LogError(exec, exec.Message); FhirVersion VersionInUse = GetFhirVersionInUse(context.Request.Path.Value); if (exec is FhirException FhirException) { _logger.LogError(FhirException, "FhirException has been thrown"); FhirFormatType AcceptFormatType = Bug.Api.ContentFormatters.FhirMediaType.GetFhirFormatTypeFromAcceptHeader(context.Request.Headers.SingleOrDefault(x => x.Key.ToLower(System.Globalization.CultureInfo.CurrentCulture) == "accept").Value); switch (VersionInUse) { case FhirVersion.Stu3: { return(Stu3FhirExceptionProcessing(context, FhirException, AcceptFormatType)); } case FhirVersion.R4: { return(R4FhirExceptionProcessing(context, FhirException, AcceptFormatType)); } default: throw new ApplicationException($"Unable to resolve which major version of FHIR is in use. Found enum: {VersionInUse.ToString()}"); } } else { string ErrorGuid = Common.FhirTools.FhirGuidSupport.NewFhirGuid(); string UsersErrorMessage = string.Empty; if (Debugger.IsAttached) { UsersErrorMessage = $"{System.Text.Encodings.Web.HtmlEncoder.Default.Encode(exec.ToString())} -> Server Error log identifier: {ErrorGuid}"; } else { UsersErrorMessage = $"An unhanded exception has been thrown. To protect data privacy the exception information has been written to the application log with the error log identifier: {ErrorGuid}"; } _logger.LogError(exec, $"Error log identifier: {ErrorGuid}"); switch (VersionInUse) { case FhirVersion.Stu3: { Stu3Model.OperationOutcome Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetFatal(new string[] { UsersErrorMessage }); context.Response.ContentType = Bug.Api.ContentFormatters.FhirMediaType.GetMediaTypeHeaderValue(Stu3OperationOutcomeResult.GetType(), FhirFormatType.xml).Value; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(context.Response.WriteAsync(IStu3SerializationToXml.SerializeToXml(Stu3OperationOutcomeResult))); } case FhirVersion.R4: { R4Model.OperationOutcome R4OperationOutcomeResult = IOperationOutComeSupportFactory.GetR4().GetFatal(new string[] { UsersErrorMessage }); context.Response.ContentType = Bug.Api.ContentFormatters.FhirMediaType.GetMediaTypeHeaderValue(R4OperationOutcomeResult.GetType(), FhirFormatType.xml).Value; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(context.Response.WriteAsync(IR4SerializationToXml.SerializeToXml(R4OperationOutcomeResult))); } default: string msg = $"Unable to resolve which major version of FHIR is in use. Found enum: {VersionInUse.ToString()}"; _logger.LogError(msg); throw new ApplicationException(msg); } } }
private Task Stu3FhirExceptionProcessing(HttpContext context, FhirException FhirException, FhirFormatType AcceptFormatType) { Stu3Model.OperationOutcome?Stu3OperationOutcomeResult; if (FhirException is FhirFatalException FatalExec) { Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetFatal(FatalExec.MessageList); } else if (FhirException is FhirErrorException ErrorExec) { Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetError(ErrorExec.MessageList); } else if (FhirException is FhirWarnException WarnExec) { Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetWarning(WarnExec.MessageList); } else if (FhirException is FhirInfoException InfoExec) { Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetInformation(InfoExec.MessageList); } else { Stu3OperationOutcomeResult = IOperationOutComeSupportFactory.GetStu3().GetFatal(new string[] { $"Unexpected FhirException type encountered of : {FhirException.GetType().FullName}" }); } context.Response.StatusCode = (int)FhirException.HttpStatusCode; context.Response.ContentType = Bug.Api.ContentFormatters.FhirMediaType.GetMediaTypeHeaderValue(Stu3OperationOutcomeResult.GetType(), AcceptFormatType).Value; if (AcceptFormatType == FhirFormatType.xml) { return(context.Response.WriteAsync(IStu3SerializationToXml.SerializeToXml(Stu3OperationOutcomeResult))); } else if (AcceptFormatType == FhirFormatType.json) { return(context.Response.WriteAsync(IStu3SerializationToJson.SerializeToJson(Stu3OperationOutcomeResult))); } else { string msg = $"Unexpected FhirFormatType type encountered of : {AcceptFormatType.GetType().FullName}"; _logger.LogError(msg); throw new ApplicationException(msg); } }