Added a default behavior for the exceptions that are not checked

This commit is contained in:
Isa Goksu
2012-12-07 12:42:15 -08:00
parent 943b3f722e
commit b49d8cc31c
2 changed files with 20 additions and 5 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();
}
private 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);
}