Improve error handling

This commit is contained in:
jamesagnew 2021-03-30 07:36:08 -04:00
parent 4feda13d7f
commit 06bdefa16c
3 changed files with 31 additions and 1 deletions

View File

@ -82,6 +82,12 @@ public class SearchPreferHandlingInterceptor {
return; return;
} }
String resourceName = theRequestDetails.getResourceName();
if (!theRequestDetails.getFhirContext().getResourceTypes().contains(resourceName)) {
// This is an error. Let the server handle it normally.
return;
}
String preferHeader = theRequestDetails.getHeader(Constants.HEADER_PREFER); String preferHeader = theRequestDetails.getHeader(Constants.HEADER_PREFER);
PreferHandlingEnum handling = null; PreferHandlingEnum handling = null;
if (isNotBlank(preferHeader)) { if (isNotBlank(preferHeader)) {

View File

@ -385,6 +385,7 @@ public class ServerCapabilityStatementProvider implements IServerConformanceProv
List<IBaseResource> allStructureDefinitions = myValidationSupport.fetchAllNonBaseStructureDefinitions(); List<IBaseResource> allStructureDefinitions = myValidationSupport.fetchAllNonBaseStructureDefinitions();
if (allStructureDefinitions != null) { if (allStructureDefinitions != null) {
for (IBaseResource next : allStructureDefinitions) { for (IBaseResource next : allStructureDefinitions) {
String id = next.getIdElement().getValue();
String kind = terser.getSinglePrimitiveValueOrNull(next, "kind"); String kind = terser.getSinglePrimitiveValueOrNull(next, "kind");
String url = terser.getSinglePrimitiveValueOrNull(next, "url"); String url = terser.getSinglePrimitiveValueOrNull(next, "url");
String baseDefinition = defaultString(terser.getSinglePrimitiveValueOrNull(next, "baseDefinition")); String baseDefinition = defaultString(terser.getSinglePrimitiveValueOrNull(next, "baseDefinition"));
@ -397,7 +398,12 @@ public class ServerCapabilityStatementProvider implements IServerConformanceProv
String resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path"); String resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path");
if (isBlank(resourceType)) { if (isBlank(resourceType)) {
try {
next = myValidationSupport.generateSnapshot(new ValidationSupportContext(myValidationSupport), next, null, null, null); next = myValidationSupport.generateSnapshot(new ValidationSupportContext(myValidationSupport), next, null, null, null);
} catch (Exception e) {
ourLog.warn("Failure while generating snapshot for StructureDefinition with URL[{}] ID[{}]", url, id, e);
continue;
}
if (next != null) { if (next != null) {
resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path"); resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path");
} }

View File

@ -14,6 +14,11 @@ import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.interceptor.SearchPreferHandlingInterceptor; import ca.uhn.fhir.rest.server.interceptor.SearchPreferHandlingInterceptor;
import ca.uhn.fhir.test.utilities.server.RestfulServerExtension; import ca.uhn.fhir.test.utilities.server.RestfulServerExtension;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.HumanName; import org.hl7.fhir.r4.model.HumanName;
@ -22,6 +27,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.extension.RegisterExtension;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -67,6 +74,17 @@ public class SearchPreferHandlingInterceptorTest {
} }
@Test
public void testSearchWithUnknownResourceType() throws IOException {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
try (CloseableHttpResponse result = client.execute(new HttpGet("http://localhost:" + myPort + "/BadResource?foo=bar"))) {
assertEquals(404, result.getStatusLine().getStatusCode());
String response = IOUtils.toString(result.getEntity().getContent(), StandardCharsets.UTF_8);
assertThat(response, containsString("Unknown resource type 'BadResource' - Server knows how to handle: [Patient, OperationDefinition]"));
}
}
}
@Test @Test
public void testSearchWithInvalidParam_StrictHeader() { public void testSearchWithInvalidParam_StrictHeader() {
try { try {