using Hl7.Fhir.Model; public static class FhirOperationOutcomeBuilder { public static OperationOutcome FromException(Exception ex) => ex switch { FhirMappingException fme => Create(fme.HttpStatus, fme.FhirIssueCode, fme.Message), NotFoundException nfe => Create(404, "not-found", nfe.Message), ValidationException ve => Create(422, "invalid", ve.Message), ConflictException ce => Create(409, "conflict", ce.Message), _ => Create(500, "exception", "An unexpected error occurred.") }; public static OperationOutcome Create(int status, string code, string diagnostics) => new() { Issue = new List { new() { Severity = status >= 500 ? OperationOutcome.IssueSeverity.Error : OperationOutcome.IssueSeverity.Warning, Code = Enum.TryParse(code, true, out var c) ? c : OperationOutcome.IssueType.Processing, Diagnostics = diagnostics } } }; }