Accoring to FHIR specification, in case of any error an OperationOutcome resource should be returned.
KeFHIR automatically takes care of this and composes OperationOutcome
in case of any exception.
You can take advantage of KefhirResponseFilter in order to perform some custom actions on error.
Yet, you can still rewrite default handling behaviour by replacing FhirExceptionHandler
bean with your own implementation. Make sure you extend this bean.
@Singleton
@Replaces(FhirExceptionHandler.class)
public class CustomExceptionHandler extends FhirExceptionHandler {
public CustomExceptionHandler(ResourceFormatService resourceFormatService) {
super(resourceFormatService);
}
@Override
public HttpResponse<?> handle(HttpRequest request, Throwable e) {
if (e instanceof MyCustomException) {
return HttpResponse.badRequest("the inevitable has happened");
}
return super.handle(request, e);
}
}
For the most readability, it is suggested to throw FhirException
in all cases you need to give client an error. This exception is structurally close to OperationOutcome
providing easy possibility to provide errors with FHIR specification.
class FhirException extends RuntimeException {
private final int httpCode;
private final List<OperationOutcomeIssueComponent> issues;
private List<Extension> extensions;
private List<Resource> contained;