Refactored and updated pom to version 2.0.1

This commit is contained in:
raychatter
2013-01-16 18:07:05 -08:00
parent bca69232cb
commit 572053174e
3 changed files with 12 additions and 16 deletions

View File

@@ -18,7 +18,7 @@
<inceptionYear>2012</inceptionYear> <inceptionYear>2012</inceptionYear>
<groupId>com.github.raychatter</groupId> <groupId>com.github.raychatter</groupId>
<artifactId>spring-restful-exception-handler</artifactId> <artifactId>spring-restful-exception-handler</artifactId>
<version>1.1.4-SNAPSHOT</version> <version>2.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@@ -67,11 +67,6 @@
<version>1.9.0</version> <version>1.9.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -18,6 +18,8 @@ public class AnnotationHandler implements HandlerExceptionResolver {
protected static final String USER_TEMPLATE = "error.template"; protected static final String USER_TEMPLATE = "error.template";
protected static final String DEFAULT_TEMPLATE = "defaults/default.template"; protected static final String DEFAULT_TEMPLATE = "defaults/default.template";
private static final String UTF_8 = "UTF-8"; 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 useHandledExceptionMessage = true; //use the message from the annotated exception
private boolean useGetCause = true; private boolean useGetCause = true;
@@ -34,8 +36,6 @@ public class AnnotationHandler implements HandlerExceptionResolver {
final Exception handledException = getHandledException(thrownException); final Exception handledException = getHandledException(thrownException);
final ExceptionHandler annotation = getAnnotationFrom(handledException); final ExceptionHandler annotation = getAnnotationFrom(handledException);
final Exception messageException = getMessageException(thrownException, handledException); final Exception messageException = getMessageException(thrownException, handledException);
// final StackTraceElement[] testStacktrace = handledException.getStackTrace();
// final List<Throwable> throwableList = ExceptionUtils.getThrowableList(thrownException);
try { try {
if (annotation == null) { if (annotation == null) {
@@ -54,7 +54,7 @@ public class AnnotationHandler implements HandlerExceptionResolver {
protected Exception getHandledException(final Exception thrownException) { protected Exception getHandledException(final Exception thrownException) {
if(useGetCause) { if(useGetCause) {
return getAnnotatedException(thrownException, thrownException.getCause()); return getAnnotatedException(thrownException);
} }
return thrownException; return thrownException;
} }
@@ -82,18 +82,19 @@ public class AnnotationHandler implements HandlerExceptionResolver {
} }
protected ModelAndView respondWithDefault(final Exception handledException, final HttpServletResponse response) throws IOException { protected ModelAndView respondWithDefault(final Exception handledException, final HttpServletResponse response) throws IOException {
response.setContentType(MediaType.APPLICATION_XML_VALUE); response.setContentType(DEFAULT_CONTENT_TYPE);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.setStatus(DEFAULT_STATUS_CODE);
response.getWriter().write(formatDefaultMessage(handledException)); response.getWriter().write(formatDefaultMessage(handledException));
return new ModelAndView(); 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) { if(getAnnotationFrom(exception) != null || causedException == null) {
return exception; return exception;
} else { } else {
return getAnnotatedException((Exception) causedException, causedException.getCause()); return getAnnotatedException((Exception) causedException);
} }
} }

View File

@@ -279,7 +279,7 @@ public class AnnotationHandlerTest {
when(mockResponse.getWriter()).thenReturn(mockPrinter); when(mockResponse.getWriter()).thenReturn(mockPrinter);
final AnnotationHandler sut = spy(new AnnotationHandler()); 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); when(sut.getAnnotationFrom(expectedException)).thenReturn(null);
final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException); 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); 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 { @Test public void getHandledException_ShouldReturnThrownException_WhenThereAreCheckedExceptionsChainedAndGetCauseIsFalse() throws Exception {
@@ -316,7 +316,7 @@ public class AnnotationHandlerTest {
final ModelAndView view = sut.resolveException(null, mockResponse, null, mockException); 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 { @Test public void getHandledException_ShouldReturnFirstChainedAnnotatedException_WhenThrownExceptionIsUnannotatedAndGetCauseIsTrue() throws Exception {