Skip to content

Commit

Permalink
Merge pull request #769 from InfiniteLoop90/issue-768-exclude-stacktr…
Browse files Browse the repository at this point in the history
…ace-jax-rs

Fixes issue #768 to conditionally include stacktraces for DataFormatExceptions in OperationOutcomes when using AbstractJaxRsProviders
  • Loading branch information
jamesagnew committed Nov 23, 2017
2 parents d77c0b9 + d0e747d commit 6293bb1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -26,21 +26,16 @@
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;
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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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));
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6293bb1

Please sign in to comment.