Merge pull request #769 from InfiniteLoop90/issue-768-exclude-stacktrace-jax-rs

Fixes issue #768 to conditionally include stacktraces for DataFormatExceptions in OperationOutcomes when using AbstractJaxRsProviders
This commit is contained in:
James Agnew 2017-11-22 20:00:44 -05:00 committed by GitHub
commit 6293bb112b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 16 deletions

View File

@ -48,6 +48,10 @@ public class InvalidRequestException extends BaseServerResponseException {
super(STATUS_CODE, theMessage); super(STATUS_CODE, theMessage);
} }
public InvalidRequestException(Throwable theCause) {
super(STATUS_CODE, theCause);
}
/** /**
* Constructor * Constructor
* *

View File

@ -26,8 +26,6 @@ import java.util.Map.Entry;
import javax.ws.rs.core.*; import javax.ws.rs.core.*;
import org.apache.commons.lang3.StringUtils; 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.FhirContext;
import ca.uhn.fhir.context.api.AddProfileTagEnum; 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.interceptor.JaxRsResponseException;
import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest; import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest;
import ca.uhn.fhir.jaxrs.server.util.JaxRsRequest.Builder; 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.api.*;
import ca.uhn.fhir.rest.server.*; 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.rest.server.interceptor.IServerInterceptor;
import ca.uhn.fhir.util.OperationOutcomeUtil;
/** /**
* This is the abstract superclass for all jaxrs providers. It contains some defaults implementing * 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; 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 * DEFAULT = AddProfileTagEnum.NEVER
*/ */
@ -241,9 +229,6 @@ public abstract class AbstractJaxRsProvider implements IRestfulServerDefaults {
throws IOException { throws IOException {
if (theException instanceof JaxRsResponseException) { if (theException instanceof JaxRsResponseException) {
return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, (JaxRsResponseException) theException); 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 { } else {
return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest, return new JaxRsExceptionInterceptor().convertExceptionIntoResponse(theRequest,
new JaxRsExceptionInterceptor().convertException(this, theException)); new JaxRsExceptionInterceptor().convertException(this, theException));

View File

@ -32,6 +32,8 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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.apache.commons.lang3.exception.ExceptionUtils;
import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
@ -99,7 +101,10 @@ public class ExceptionHandlingInterceptor extends InterceptorAdapter {
@Override @Override
public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException { public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException {
BaseServerResponseException retVal; 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); retVal = new InternalErrorException(theException);
} else { } else {
retVal = (BaseServerResponseException) theException; retVal = (BaseServerResponseException) theException;