Compare commits
33 Commits
spring-res
...
spring-res
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33a95e1d40 | ||
|
|
d998bfe694 | ||
|
|
67698be443 | ||
|
|
78b2c8a3b9 | ||
|
|
528a58d881 | ||
|
|
78d9cb2ca8 | ||
|
|
d4e9474f13 | ||
|
|
84d3407507 | ||
|
|
7f8ddd5157 | ||
|
|
af6d3d1596 | ||
|
|
90799b3a51 | ||
|
|
4ab8856d8b | ||
|
|
d27a26fa8c | ||
|
|
9010542e85 | ||
|
|
b74ccb0627 | ||
|
|
4cd8bbdaa4 | ||
|
|
ef7d2b35a4 | ||
|
|
61ff23fac4 | ||
|
|
5c8d5a1289 | ||
|
|
62801050cd | ||
|
|
9f58c840cd | ||
|
|
a61e09f1b3 | ||
|
|
4205cc8805 | ||
|
|
138d0e78be | ||
|
|
88396fcd75 | ||
|
|
551b28c66d | ||
|
|
f49b17393c | ||
|
|
ebf8913f47 | ||
|
|
93b9c352e4 | ||
|
|
c93940b7fa | ||
|
|
ff02eaf3de | ||
|
|
10559a8d25 | ||
|
|
6b289c96d5 |
@@ -46,3 +46,7 @@ public class MyCustomException extends Exception {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Note:
|
||||
|
||||
If an unannotated exception is thrown, the spring-restful-exception-handler will call `getCause()` on the exception until the first annotated exception is found. Provided there is no annotated exception, the default httpStatus and contentType will be used.
|
||||
|
||||
20
pom.xml
20
pom.xml
@@ -18,7 +18,7 @@
|
||||
<inceptionYear>2012</inceptionYear>
|
||||
<groupId>com.github.raychatter</groupId>
|
||||
<artifactId>spring-restful-exception-handler</artifactId>
|
||||
<version>1.0.3</version>
|
||||
<version>1.1.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -94,9 +94,6 @@
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>no-dependencies</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
@@ -182,19 +179,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>1.7</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
@@ -248,7 +232,7 @@
|
||||
<connection>scm:git:git@github.com:raychatter/spring-restful-exception-handler.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:raychatter/spring-restful-exception-handler.git</developerConnection>
|
||||
<url>git@github.com:raychatter/spring-restful-exception-handler.git</url>
|
||||
<tag>spring-restful-exception-handler-1.0.3</tag>
|
||||
<tag>spring-restful-exception-handler-1.1.1</tag>
|
||||
</scm>
|
||||
|
||||
<issueManagement>
|
||||
|
||||
@@ -19,20 +19,19 @@ public class AnnotationHandler implements HandlerExceptionResolver {
|
||||
protected static final String DEFAULT_TEMPLATE = "defaults/default.template";
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
|
||||
//TODO: When there's a wrapper exception leave it unannotated… call the e.getCause() method… until there is an annotated exception or if there are no more causes (e.geCause()==null)??.
|
||||
@Override
|
||||
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception thrownException) {
|
||||
final ExceptionHandler annotation = getAnnotationFrom(thrownException);
|
||||
Exception rootException = getAnnotatedException(thrownException, thrownException.getCause());
|
||||
final ExceptionHandler annotation = getAnnotationFrom(rootException);
|
||||
|
||||
try {
|
||||
if (annotation == null) {
|
||||
thrownException.printStackTrace();
|
||||
return respondWithDefault(thrownException, response);
|
||||
rootException.printStackTrace();
|
||||
return respondWithDefault(rootException, response);
|
||||
}
|
||||
|
||||
return handleException(annotation, thrownException, response);
|
||||
return handleException(annotation, rootException, response);
|
||||
} catch (IOException e) {
|
||||
// potentially something went wrong in response itself
|
||||
// potentially something went wrong in the response itself
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -61,8 +60,16 @@ public class AnnotationHandler implements HandlerExceptionResolver {
|
||||
return new ModelAndView();
|
||||
}
|
||||
|
||||
protected ExceptionHandler getAnnotationFrom(Exception thrownException) {
|
||||
return thrownException.getClass().getAnnotation(ExceptionHandler.class);
|
||||
protected Exception getAnnotatedException(Exception thrownException, Throwable causedException) {
|
||||
if(getAnnotationFrom(thrownException) != null || causedException == null) {
|
||||
return thrownException;
|
||||
} else {
|
||||
return getAnnotatedException((Exception) causedException, causedException.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
protected ExceptionHandler getAnnotationFrom(Exception exception) {
|
||||
return exception.getClass().getAnnotation(ExceptionHandler.class);
|
||||
}
|
||||
|
||||
protected String formatMessage(final Exception thrownException) throws IOException {
|
||||
|
||||
@@ -13,7 +13,12 @@ import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AnnotationHandlerTest {
|
||||
|
||||
@@ -234,6 +239,7 @@ public class AnnotationHandlerTest {
|
||||
|
||||
final AnnotationHandler sut = spy(new AnnotationHandler());
|
||||
doReturn(new ModelAndView()).when(sut).handleException(mockAnnotation, expectedException, mockResponse);
|
||||
doReturn(expectedException).when(sut).getAnnotatedException(expectedException, expectedException.getCause());
|
||||
doReturn(mockAnnotation).when(sut).getAnnotationFrom(expectedException);
|
||||
|
||||
final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException);
|
||||
@@ -249,12 +255,46 @@ public class AnnotationHandlerTest {
|
||||
when(mockResponse.getWriter()).thenReturn(mockPrinter);
|
||||
|
||||
final AnnotationHandler sut = spy(new AnnotationHandler());
|
||||
doReturn(null).when(sut).getAnnotationFrom(expectedException);
|
||||
when(sut.getAnnotatedException(expectedException, expectedException.getCause())).thenReturn(expectedException);
|
||||
when(sut.getAnnotationFrom(expectedException)).thenReturn(null);
|
||||
|
||||
final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException);
|
||||
|
||||
verify(sut).respondWithDefault(expectedException, mockResponse);
|
||||
}
|
||||
|
||||
@Test public void getAnnotatedException_ShouldReturnOutermostAnnotatedException_WhenThereAreCheckedExceptionsChained() throws Exception {
|
||||
TestExceptionWithNoAnnotationAttributes mockException = mock(TestExceptionWithNoAnnotationAttributes.class);
|
||||
doReturn(new TestExceptionWithXmlContentType()).when(mockException).getCause();
|
||||
|
||||
final ExceptionHandler mockAnnotation = mock(ExceptionHandler.class);
|
||||
final HttpServletResponse mockResponse = mock(HttpServletResponse.class);
|
||||
|
||||
final AnnotationHandler sut = spy(new AnnotationHandler());
|
||||
when(sut.getAnnotationFrom(mockException)).thenReturn(mockAnnotation);
|
||||
doReturn(new ModelAndView()).when(sut).handleException(mockAnnotation, mockException, mockResponse);
|
||||
|
||||
final ModelAndView view = sut.resolveException(null, mockResponse, null, mockException);
|
||||
|
||||
Assert.assertTrue(sut.getAnnotatedException(mockException,mockException.getCause()) instanceof TestExceptionWithNoAnnotationAttributes);
|
||||
}
|
||||
|
||||
@Test public void getAnnotatedException_ShouldReturnFirstChainedAnnotatedException_WhenThrownExceptionIsUnannotated() throws Exception {
|
||||
TestExceptionWithNoAnnotation mockException = mock(TestExceptionWithNoAnnotation.class);
|
||||
TestExceptionWithNoAnnotationAttributes expectedException = new TestExceptionWithNoAnnotationAttributes("");
|
||||
doReturn(expectedException).when(mockException).getCause();
|
||||
|
||||
final ExceptionHandler mockAnnotation = mock(ExceptionHandler.class);
|
||||
final HttpServletResponse mockResponse = mock(HttpServletResponse.class);
|
||||
|
||||
final AnnotationHandler sut = spy(new AnnotationHandler());
|
||||
when(sut.getAnnotationFrom(expectedException)).thenReturn(mockAnnotation);
|
||||
doReturn(new ModelAndView()).when(sut).handleException(mockAnnotation, expectedException, mockResponse);
|
||||
|
||||
final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException);
|
||||
|
||||
Assert.assertTrue(sut.getAnnotatedException(mockException,mockException.getCause()) instanceof TestExceptionWithNoAnnotationAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
@ExceptionHandler()
|
||||
@@ -279,3 +319,8 @@ class TestExceptionWithNoContentStatusCodeAndTextContentType extends Exception {
|
||||
}
|
||||
}
|
||||
|
||||
class TestExceptionWithNoAnnotation extends Exception {
|
||||
public TestExceptionWithNoAnnotation(final String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user