31 lines
1.2 KiB
C#
31 lines
1.2 KiB
C#
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<OperationOutcome.IssueComponent>
|
|
{
|
|
new()
|
|
{
|
|
Severity = status >= 500
|
|
? OperationOutcome.IssueSeverity.Error
|
|
: OperationOutcome.IssueSeverity.Warning,
|
|
Code = Enum.TryParse<OperationOutcome.IssueType>(code, true, out var c)
|
|
? c
|
|
: OperationOutcome.IssueType.Processing,
|
|
Diagnostics = diagnostics
|
|
}
|
|
}
|
|
};
|
|
} |