Don't suppress exceptions in clients

This commit is contained in:
Grahame Grieve 2023-08-19 20:15:29 +10:00
parent ae8e412009
commit 2acf546059
6 changed files with 136 additions and 115 deletions

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.convertors.txClient;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.HashMap;
@ -8,10 +9,12 @@ import java.util.Map;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.utils.client.EFhirClientException;
import org.hl7.fhir.r4.utils.client.FHIRToolingClient;
import org.hl7.fhir.r5.model.Bundle;
import org.hl7.fhir.r5.model.CanonicalResource;
import org.hl7.fhir.r5.model.CapabilityStatement;
import org.hl7.fhir.r5.model.OperationOutcome;
import org.hl7.fhir.r5.model.Parameters;
import org.hl7.fhir.r5.model.TerminologyCapabilities;
import org.hl7.fhir.r5.model.ValueSet;
@ -93,16 +96,38 @@ public class TerminologyClientR4 implements ITerminologyClient {
@Override
public Parameters validateCS(Parameters pin) throws FHIRException {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
try {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
} catch (EFhirClientException e) {
if (e.getServerErrors().size() == 1) {
OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getMessage(), op, e);
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getMessage(), e);
}
} catch (IOException e) {
throw new FHIRException(e);
}
}
@Override
public Parameters validateVS(Parameters pin) throws FHIRException {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
p2 = client.operateType(org.hl7.fhir.r4.model.ValueSet.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
try {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
p2 = client.operateType(org.hl7.fhir.r4.model.ValueSet.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
} catch (EFhirClientException e) {
if (e.getServerErrors().size() == 1) {
OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getMessage(), op, e);
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getMessage(), e);
}
} catch (IOException e) {
throw new FHIRException(e);
}
}
@Override

View File

@ -522,37 +522,31 @@ public class FHIRToolingClient {
complex = complex || !(p.getValue() instanceof PrimitiveType);
Parameters searchResults = null;
String ps = "";
try {
if (!complex)
for (ParametersParameterComponent p : params.getParameter())
if (p.getValue() instanceof PrimitiveType)
ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&";
ResourceRequest<T> result;
if (complex)
result = utils.issuePostRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps),
utils.getResourceAsByteArray(params, false, isJson(getPreferredResourceFormat())),
getPreferredResourceFormat(), TIMEOUT_OPERATION_LONG);
else
result = utils.issueGetResourceRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps),
getPreferredResourceFormat(), TIMEOUT_OPERATION_LONG);
result.addErrorStatus(410);// gone
result.addErrorStatus(404);// unknown
result.addSuccessStatus(200);// Only one for now
if (result.isUnsuccessfulRequest())
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
(OperationOutcome) result.getPayload());
if (result.getPayload() instanceof Parameters)
return (Parameters) result.getPayload();
else {
Parameters p_out = new Parameters();
p_out.addParameter().setName("return").setResource(result.getPayload());
return p_out;
}
} catch (Exception e) {
handleException(
"Error performing tx2 operation '" + name + ": " + e.getMessage() + "' (parameters = \"" + ps + "\")", e);
if (!complex)
for (ParametersParameterComponent p : params.getParameter())
if (p.getValue() instanceof PrimitiveType)
ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&";
ResourceRequest<T> result;
if (complex)
result = utils.issuePostRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps),
utils.getResourceAsByteArray(params, false, isJson(getPreferredResourceFormat())),
getPreferredResourceFormat(), TIMEOUT_OPERATION_LONG);
else
result = utils.issueGetResourceRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps),
getPreferredResourceFormat(), TIMEOUT_OPERATION_LONG);
result.addErrorStatus(410);// gone
result.addErrorStatus(404);// unknown
result.addSuccessStatus(200);// Only one for now
if (result.isUnsuccessfulRequest())
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
(OperationOutcome) result.getPayload());
if (result.getPayload() instanceof Parameters)
return (Parameters) result.getPayload();
else {
Parameters p_out = new Parameters();
p_out.addParameter().setName("return").setResource(result.getPayload());
return p_out;
}
return null;
}
public Bundle transaction(Bundle batch) {

View File

@ -1,5 +1,7 @@
package org.hl7.fhir.r4.terminologies;
import java.io.IOException;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
@ -29,9 +31,11 @@ package org.hl7.fhir.r4.terminologies;
*/
import java.net.URISyntaxException;
import java.util.Map;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.context.HTMLClientLogger;
import org.hl7.fhir.r4.model.CapabilityStatement;
import org.hl7.fhir.r4.model.CodeSystem;
@ -65,12 +69,20 @@ public class TerminologyClientR4 implements TerminologyClient {
@Override
public Parameters validateCS(Parameters pin) {
return client.operateType(CodeSystem.class, "validate-code", pin);
try {
return client.operateType(CodeSystem.class, "validate-code", pin);
} catch (IOException e) {
throw new FHIRException(e);
}
}
@Override
public Parameters validateVS(Parameters pin) {
return client.operateType(ValueSet.class, "validate-code", pin);
try {
return client.operateType(ValueSet.class, "validate-code", pin);
} catch (IOException e) {
throw new FHIRException(e);
}
}
@Override

View File

@ -278,42 +278,36 @@ public class FHIRToolingClient {
return result.getPayload();
}
public <T extends Resource> Parameters operateType(Class<T> resourceClass, String name, Parameters params) {
public <T extends Resource> Parameters operateType(Class<T> resourceClass, String name, Parameters params) throws IOException {
boolean complex = false;
for (ParametersParameterComponent p : params.getParameter())
complex = complex || !(p.getValue() instanceof PrimitiveType);
String ps = "";
try {
if (!complex)
for (ParametersParameterComponent p : params.getParameter())
if (p.getValue() instanceof PrimitiveType)
ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&";
ResourceRequest<T> result;
URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
if (complex) {
byte[] body = ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()));
result = client.issuePostRequest(url, body, getPreferredResourceFormat(), generateHeaders(),
"POST " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
} else {
result = client.issueGetResourceRequest(url, getPreferredResourceFormat(), generateHeaders(),
"GET " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
(OperationOutcome) result.getPayload());
}
if (result.getPayload() instanceof Parameters) {
return (Parameters) result.getPayload();
} else {
Parameters p_out = new Parameters();
p_out.addParameter().setName("return").setResource(result.getPayload());
return p_out;
}
} catch (Exception e) {
handleException(
"Error performing tx4 operation '" + name + ": " + e.getMessage() + "' (parameters = \"" + ps + "\")", e);
if (!complex)
for (ParametersParameterComponent p : params.getParameter())
if (p.getValue() instanceof PrimitiveType)
ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&";
ResourceRequest<T> result;
URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
if (complex) {
byte[] body = ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()));
result = client.issuePostRequest(url, body, getPreferredResourceFormat(), generateHeaders(),
"POST " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
} else {
result = client.issueGetResourceRequest(url, getPreferredResourceFormat(), generateHeaders(),
"GET " + resourceClass.getName() + "/$" + name, TIMEOUT_OPERATION_LONG);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
(OperationOutcome) result.getPayload());
}
if (result.getPayload() instanceof Parameters) {
return (Parameters) result.getPayload();
} else {
Parameters p_out = new Parameters();
p_out.addParameter().setName("return").setResource(result.getPayload());
return p_out;
}
return null;
}
public Bundle transaction(Bundle batch) {
@ -390,16 +384,16 @@ public class FHIRToolingClient {
parameters.put("url", vsUrl);
org.hl7.fhir.r4.utils.client.network.ResourceRequest<Resource> result = null;
try {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand", parameters),
getPreferredResourceFormat(), generateHeaders(), "ValueSet/$expand?url=" + vsUrl, TIMEOUT_OPERATION_EXPAND);
try {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand", parameters),
getPreferredResourceFormat(), generateHeaders(), "ValueSet/$expand?url=" + vsUrl, TIMEOUT_OPERATION_EXPAND);
} catch (IOException e) {
throw new FHIRException(e);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
(OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
}
return result == null ? null : (ValueSet) result.getPayload();
}
@ -416,7 +410,7 @@ public class FHIRToolingClient {
(OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
return result == null ? null : (ValueSet) result.getPayload();
}
@ -427,7 +421,7 @@ public class FHIRToolingClient {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup", params),
getPreferredResourceFormat(), generateHeaders(), "CodeSystem/$lookup", TIMEOUT_NORMAL);
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
@ -455,7 +449,7 @@ public class FHIRToolingClient {
(OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
return result == null ? null : (ValueSet) result.getPayload();
}
@ -478,7 +472,7 @@ public class FHIRToolingClient {
(OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
return result == null ? null : (ConceptMap) result.getPayload();
}
@ -498,7 +492,7 @@ public class FHIRToolingClient {
(OperationOutcome) result.getPayload());
}
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
return result == null ? null : (ConceptMap) result.getPayload();
}

View File

@ -57,19 +57,12 @@ import org.hl7.fhir.r5.model.OperationOutcome;
*/
public class EFhirClientException extends RuntimeException {
private static final long serialVersionUID = 1L;
private List<OperationOutcome> errors = new ArrayList<OperationOutcome>();
private OperationOutcome error = null;
public EFhirClientException(String message) {
super(message);
}
public EFhirClientException(String message, List<OperationOutcome> serverErrors) {
super(message);
if(serverErrors != null && serverErrors.size() > 0) {
errors.addAll(serverErrors);
}
}
public EFhirClientException(Exception cause) {
super(cause);
}
@ -86,12 +79,23 @@ public class EFhirClientException extends RuntimeException {
* @param serverError
*/
public EFhirClientException(String message, OperationOutcome serverError) {
super(message);
if(serverError != null) {
errors.add(serverError);
}
super(message);
error = serverError;
}
/**
* Generate EFhirClientException which include a message indicating the cause of the exception
* along with any OperationOutcome server error that may have resulted.
*
* @param message
* @param serverError
*/
public EFhirClientException(String message, OperationOutcome serverError, Exception cause) {
super(message, cause);
error = serverError;
}
/**
* Generate EFhirClientException indicating the cause of the exception
* along with any OperationOutcome server error the server may have generated.
@ -103,19 +107,11 @@ public class EFhirClientException extends RuntimeException {
*/
public EFhirClientException(OperationOutcome serverError) {
super("Error on the server: "+serverError.getText().getDiv().allText()+". Refer to e.getServerErrors() for additional details.");
if(serverError != null) {
errors.add(serverError);
}
error = serverError;
}
/**
* Method returns all OperationOutcome server errors that are
* associated with this exception.
*
* @return
*/
public List<OperationOutcome> getServerErrors() {
return errors;
public OperationOutcome getServerError() {
return error;
}
/**
@ -123,8 +119,8 @@ public class EFhirClientException extends RuntimeException {
*
* @return
*/
public boolean hasServerErrors() {
return errors.size() > 0;
public boolean hasServerError() {
return error != null;
}
}

View File

@ -421,16 +421,16 @@ public class FHIRToolingClient {
org.hl7.fhir.r5.utils.client.network.ResourceRequest<Resource> result = null;
try {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand"),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat())),
getPreferredResourceFormat(),
generateHeaders(),
"ValueSet/$expand?url=" + source.getUrl(),
TIMEOUT_OPERATION_EXPAND);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat())),
getPreferredResourceFormat(),
generateHeaders(),
"ValueSet/$expand?url=" + source.getUrl(),
TIMEOUT_OPERATION_EXPAND);
} catch (IOException e) {
e.printStackTrace();
throw new FHIRException(e);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
}
return result == null ? null : (ValueSet) result.getPayload();
}