diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/exceptions/InvalidRequestException.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/exceptions/InvalidRequestException.java index 31a51f292aba..e56d3abd620f 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/exceptions/InvalidRequestException.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/exceptions/InvalidRequestException.java @@ -47,6 +47,10 @@ public class InvalidRequestException extends BaseServerResponseException { public InvalidRequestException(String theMessage) { super(STATUS_CODE, theMessage); } + + public InvalidRequestException(Throwable theCause) { + super(STATUS_CODE, theCause); + } /** * Constructor diff --git a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsProvider.java b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsProvider.java index 5ba4d6a3a54c..a0194aa1451d 100644 --- a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsProvider.java +++ b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsProvider.java @@ -26,8 +26,6 @@ import javax.ws.rs.core.*; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.api.AddProfileTagEnum; @@ -35,12 +33,9 @@ import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsResponseException; import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest; import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest.Builder; -import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.server.*; -import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; -import ca.uhn.fhir.util.OperationOutcomeUtil; /** * This is the abstract superclass for all jaxrs providers. It contains some defaults implementing @@ -79,13 +74,6 @@ protected AbstractJaxRsProvider(final FhirContext ctx) { CTX = ctx; } - private IBaseOperationOutcome createOutcome(final DataFormatException theException) { - final IBaseOperationOutcome oo = OperationOutcomeUtil.newInstance(getFhirContext()); - final String detailsValue = theException.getMessage() + "\n\n" + ExceptionUtils.getStackTrace(theException); - OperationOutcomeUtil.addIssue(getFhirContext(), oo, ERROR, detailsValue, null, PROCESSING); - return oo; - } - /** * DEFAULT = AddProfileTagEnum.NEVER */ @@ -241,9 +229,6 @@ public Response handleException(final JaxRsRequest theRequest, final Throwable t throws IOException { if (theException instanceof JaxRsResponseException) { return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, (JaxRsResponseException) theException); - } else if (theException instanceof DataFormatException) { - return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, new JaxRsResponseException( - new InvalidRequestException(theException.getMessage(), createOutcome((DataFormatException) theException)))); } else { return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, new JaxRsExceptionInterceptor().convertException(this, theException)); diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptor.java index 55d61dc93e7c..da2bba0e0534 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptor.java @@ -32,6 +32,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import ca.uhn.fhir.parser.DataFormatException; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import org.apache.commons.lang3.exception.ExceptionUtils; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; @@ -99,7 +101,10 @@ public Object handleException(RequestDetails theRequestDetails, BaseServerRespon @Override public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException { BaseServerResponseException retVal; - if (!(theException instanceof BaseServerResponseException)) { + if (theException instanceof DataFormatException) { + // Wrapping the DataFormatException as an InvalidRequestException so that it gets sent back to the client as a 400 response. + retVal = new InvalidRequestException(theException); + } else if (!(theException instanceof BaseServerResponseException)) { retVal = new InternalErrorException(theException); } else { retVal = (BaseServerResponseException) theException;