ussmith closed pull request #7: Allow Provider implementations to handle JAX-RS exceptions by default URL: https://github.com/apache/directory-scimple/pull/7
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pom.xml b/pom.xml index 3187b5f..5d51a6a 100644 --- a/pom.xml +++ b/pom.xml @@ -238,6 +238,18 @@ <artifactId>com.eclipsesource.restfuse</artifactId> <version>${version.restfuse}</version> </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <version>2.0.0.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>eu.codearte.catch-exception</groupId> + <artifactId>catch-exception</artifactId> + <version>1.4.6</version> + <scope>test</scope> + </dependency> <dependency> <groupId>com.google.gwt</groupId> <artifactId>gwt</artifactId> diff --git a/scim-server/scim-server-common/pom.xml b/scim-server/scim-server-common/pom.xml index 6bbf8ee..8ba3af1 100644 --- a/scim-server/scim-server-common/pom.xml +++ b/scim-server/scim-server-common/pom.xml @@ -89,6 +89,16 @@ <artifactId>slf4j-simple</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>eu.codearte.catch-exception</groupId> + <artifactId>catch-exception</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> diff --git a/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/provider/Provider.java b/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/provider/Provider.java index 563cfc5..383b58f 100644 --- a/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/provider/Provider.java +++ b/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/provider/Provider.java @@ -21,6 +21,7 @@ import java.util.List; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; @@ -135,6 +136,10 @@ * @return */ default Response handleException(Throwable unhandled) { + // Allow for ErrorMessageViolationExceptionMapper to handle JAX-RS exceptions by default + if (unhandled instanceof WebApplicationException) { + throw (WebApplicationException) unhandled; + } return BaseResourceTypeResourceImpl.createGenericExceptionResponse(unhandled, Status.INTERNAL_SERVER_ERROR); } } diff --git a/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/rest/BaseResourceTypeResourceImpl.java b/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/rest/BaseResourceTypeResourceImpl.java index 6ce12d1..29b54c0 100644 --- a/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/rest/BaseResourceTypeResourceImpl.java +++ b/scim-server/scim-server-common/src/main/java/org/apache/directory/scim/server/rest/BaseResourceTypeResourceImpl.java @@ -708,7 +708,7 @@ public static Response createGenericExceptionResponse(Throwable e1, Status statu myStatus = Status.INTERNAL_SERVER_ERROR; } - ErrorResponse er = new ErrorResponse(myStatus, e1.getMessage()); + ErrorResponse er = new ErrorResponse(myStatus, e1 != null ? e1.getMessage() : "Unknown Server Error"); return er.toResponse(); } diff --git a/scim-server/scim-server-common/src/test/java/org/apache/directory/scim/server/provider/ProviderTest.java b/scim-server/scim-server-common/src/test/java/org/apache/directory/scim/server/provider/ProviderTest.java new file mode 100644 index 0000000..012223b --- /dev/null +++ b/scim-server/scim-server-common/src/test/java/org/apache/directory/scim/server/provider/ProviderTest.java @@ -0,0 +1,82 @@ +package org.apache.directory.scim.server.provider; + +import org.apache.directory.scim.spec.protocol.data.ErrorResponse; +import org.apache.directory.scim.spec.protocol.filter.FilterResponse; +import org.apache.directory.scim.spec.protocol.search.Filter; +import org.apache.directory.scim.spec.protocol.search.PageRequest; +import org.apache.directory.scim.spec.protocol.search.SortRequest; +import org.apache.directory.scim.spec.resources.ScimExtension; +import org.apache.directory.scim.spec.resources.ScimResource; +import org.junit.Test; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; + +import static com.googlecode.catchexception.CatchException.catchException; +import static com.googlecode.catchexception.CatchException.caughtException; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; + +public class ProviderTest { + + @Test + public void handleException_jaxrsExceptionTest() { + + Exception e = new WebApplicationException(); + catchException(new ProviderAdapter()).handleException(e); + assertThat(caughtException(), sameInstance(e)); + } + + @Test + public void handleException_runtimeExceptionTest() { + + Exception e = new RuntimeException("fake test exception"); + Response response = new ProviderAdapter().handleException(e); + assertThat(response.getStatus(), is(500)); + assertThat(((ErrorResponse)response.getEntity()).getDetail(), is("fake test exception")); + } + + @Test + public void handleException_nullExceptionTest() { + + Response response = new ProviderAdapter().handleException(null); + assertThat(response.getStatus(), is(500)); + assertThat(((ErrorResponse)response.getEntity()).getDetail(), is("Unknown Server Error")); + } + + private class ProviderAdapter implements Provider { + + @Override + public ScimResource create(ScimResource resource) { + return null; + } + + @Override + public ScimResource update(UpdateRequest updateRequest) { + return null; + } + + @Override + public ScimResource get(String id) { + return null; + } + + @Override + public FilterResponse find(Filter filter, PageRequest pageRequest, SortRequest sortRequest) { + return null; + } + + @Override + public void delete(String id) { + + } + + @Override + public List<Class<? extends ScimExtension>> getExtensionList() { + return null; + } + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services