diff --git a/pom.xml b/pom.xml index a4bc58c..3cebfe3 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ 2012 com.github.raychatter spring-restful-exception-handler - 1.1.4-SNAPSHOT + 2.0.1-SNAPSHOT jar @@ -67,11 +67,6 @@ 1.9.0 test - - org.apache.commons - commons-lang3 - 3.1 - diff --git a/src/main/java/com/github/raychatter/AnnotationHandler.java b/src/main/java/com/github/raychatter/AnnotationHandler.java index 365cb6d..1895922 100644 --- a/src/main/java/com/github/raychatter/AnnotationHandler.java +++ b/src/main/java/com/github/raychatter/AnnotationHandler.java @@ -18,6 +18,8 @@ public class AnnotationHandler implements HandlerExceptionResolver { protected static final String USER_TEMPLATE = "error.template"; protected static final String DEFAULT_TEMPLATE = "defaults/default.template"; private static final String UTF_8 = "UTF-8"; + private static final String DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_XML_VALUE; + private static final int DEFAULT_STATUS_CODE = HttpStatus.INTERNAL_SERVER_ERROR.value(); private boolean useHandledExceptionMessage = true; //use the message from the annotated exception private boolean useGetCause = true; @@ -34,8 +36,6 @@ public class AnnotationHandler implements HandlerExceptionResolver { final Exception handledException = getHandledException(thrownException); final ExceptionHandler annotation = getAnnotationFrom(handledException); final Exception messageException = getMessageException(thrownException, handledException); -// final StackTraceElement[] testStacktrace = handledException.getStackTrace(); -// final List throwableList = ExceptionUtils.getThrowableList(thrownException); try { if (annotation == null) { @@ -54,7 +54,7 @@ public class AnnotationHandler implements HandlerExceptionResolver { protected Exception getHandledException(final Exception thrownException) { if(useGetCause) { - return getAnnotatedException(thrownException, thrownException.getCause()); + return getAnnotatedException(thrownException); } return thrownException; } @@ -82,18 +82,19 @@ public class AnnotationHandler implements HandlerExceptionResolver { } protected ModelAndView respondWithDefault(final Exception handledException, final HttpServletResponse response) throws IOException { - response.setContentType(MediaType.APPLICATION_XML_VALUE); - response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.setContentType(DEFAULT_CONTENT_TYPE); + response.setStatus(DEFAULT_STATUS_CODE); response.getWriter().write(formatDefaultMessage(handledException)); return new ModelAndView(); } - protected Exception getAnnotatedException(Exception exception, Throwable causedException) { + protected Exception getAnnotatedException(Exception exception) { + Throwable causedException = exception.getCause(); if(getAnnotationFrom(exception) != null || causedException == null) { return exception; } else { - return getAnnotatedException((Exception) causedException, causedException.getCause()); + return getAnnotatedException((Exception) causedException); } } diff --git a/src/test/java/com/github/raychatter/AnnotationHandlerTest.java b/src/test/java/com/github/raychatter/AnnotationHandlerTest.java index 567eccd..9a9ab21 100644 --- a/src/test/java/com/github/raychatter/AnnotationHandlerTest.java +++ b/src/test/java/com/github/raychatter/AnnotationHandlerTest.java @@ -279,7 +279,7 @@ public class AnnotationHandlerTest { when(mockResponse.getWriter()).thenReturn(mockPrinter); final AnnotationHandler sut = spy(new AnnotationHandler()); - when(sut.getAnnotatedException(expectedException, expectedException.getCause())).thenReturn(expectedException); + when(sut.getAnnotatedException(expectedException)).thenReturn(expectedException); when(sut.getAnnotationFrom(expectedException)).thenReturn(null); final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException); @@ -300,7 +300,7 @@ public class AnnotationHandlerTest { final ModelAndView view = sut.resolveException(null, mockResponse, null, mockException); - Assert.assertTrue(sut.getAnnotatedException(mockException,mockException.getCause()) instanceof TestExceptionWithNoAnnotationAttributes); + Assert.assertTrue(sut.getAnnotatedException(mockException) instanceof TestExceptionWithNoAnnotationAttributes); } @Test public void getHandledException_ShouldReturnThrownException_WhenThereAreCheckedExceptionsChainedAndGetCauseIsFalse() throws Exception { @@ -316,7 +316,7 @@ public class AnnotationHandlerTest { final ModelAndView view = sut.resolveException(null, mockResponse, null, mockException); - Assert.assertTrue(sut.getAnnotatedException(mockException,mockException.getCause()) instanceof TestExceptionWithNoAnnotationAttributes); + Assert.assertTrue(sut.getAnnotatedException(mockException) instanceof TestExceptionWithNoAnnotationAttributes); } @Test public void getHandledException_ShouldReturnFirstChainedAnnotatedException_WhenThrownExceptionIsUnannotatedAndGetCauseIsTrue() throws Exception {