Changed the package names and modified the README

This commit is contained in:
Rachel Walker
2012-12-11 16:29:30 -08:00
4 changed files with 75 additions and 13 deletions

View File

@@ -23,9 +23,12 @@ public class AnnotationHandler implements HandlerExceptionResolver {
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception thrownException) {
final ExceptionHandler annotation = getAnnotationFrom(thrownException);
if (annotation == null) return new ModelAndView();
try {
if (annotation == null) {
thrownException.printStackTrace();
return respondWithDefault(thrownException, response);
}
return handleException(annotation, thrownException, response);
} catch (IOException e) {
// potentially something went wrong in response itself
@@ -43,14 +46,20 @@ public class AnnotationHandler implements HandlerExceptionResolver {
final String message = formatMessage(thrownException);
response.getWriter().write(message);
} catch (IOException e) {
response.setContentType(MediaType.APPLICATION_XML_VALUE);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.getWriter().write(formatDefaultMessage(thrownException));
return respondWithDefault(thrownException, response);
}
return new ModelAndView();
}
protected ModelAndView respondWithDefault(final Exception thrownException, final HttpServletResponse response) throws IOException {
response.setContentType(MediaType.APPLICATION_XML_VALUE);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.getWriter().write(formatDefaultMessage(thrownException));
return new ModelAndView();
}
protected ExceptionHandler getAnnotationFrom(Exception thrownException) {
return thrownException.getClass().getAnnotation(ExceptionHandler.class);
}

View File

@@ -240,6 +240,21 @@ public class AnnotationHandlerTest {
verify(sut).handleException(mockAnnotation, expectedException, mockResponse);
}
@Test public void resolveException_ShouldReturnDefaultErrorMessage_WhenUncheckedExceptionIsGiven() throws Exception {
final NullPointerException expectedException = mock(NullPointerException.class);
final HttpServletResponse mockResponse = mock(HttpServletResponse.class);
final PrintWriter mockPrinter = mock(PrintWriter.class);
when(mockResponse.getWriter()).thenReturn(mockPrinter);
final AnnotationHandler sut = spy(new AnnotationHandler());
doReturn(null).when(sut).getAnnotationFrom(expectedException);
final ModelAndView view = sut.resolveException(null, mockResponse, null, expectedException);
verify(sut).respondWithDefault(expectedException, mockResponse);
}
}
@ExceptionHandler()