Fixed issue #768 in a different way by wrapping the DataFormatException as an InvalidRequestException so that it still gets sent as a 400 response but now DataFormatException stacktraces can now be conditionally sent based on the ExceptionHandlingInterceptor#setReturnStackTracesForExceptionTypes(Class<?>...) configuration

This commit is contained in:
Clayton Bodendein 2017-10-22 22:20:48 -05:00
parent b015413961
commit d0e747d6ec
3 changed files with 10 additions and 16 deletions

View File

@ -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

View File

@ -26,8 +26,6 @@ import java.util.Map.Entry;
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.JaxRsExceptionInterceptor;
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 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
CTX = ctx;
}
private IBaseOperationOutcome createOutcome(final DataFormatException theException) {
final IBaseOperationOutcome oo = OperationOutcomeUtil.newInstance(getFhirContext());
final String detailsValue = theException.getMessage() + (this.withStackTrace() ? "\n\n" + ExceptionUtils.getStackTrace(theException) : "");
OperationOutcomeUtil.addIssue(getFhirContext(), oo, ERROR, detailsValue, null, PROCESSING);
return oo;
}
/**
* DEFAULT = AddProfileTagEnum.NEVER
*/
@ -241,9 +229,6 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
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));

View File

@ -32,6 +32,8 @@ import javax.servlet.ServletException;
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 class ExceptionHandlingInterceptor extends InterceptorAdapter {
@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;