From b49d8cc31cac77f4928fd61c13515b96234f3457 Mon Sep 17 00:00:00 2001 From: Isa Goksu Date: Fri, 7 Dec 2012 12:42:15 -0800 Subject: [PATCH] Added a default behavior for the exceptions that are not checked --- .../common/annotation/AnnotationHandler.java | 19 ++++++++++++++----- .../common/controller/HelloController.java | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java b/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java index 353b05f..12ce0ca 100644 --- a/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java +++ b/exception-handler/src/main/java/com/raychatter/common/annotation/AnnotationHandler.java @@ -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); } diff --git a/sample/src/main/java/com/raychatter/common/controller/HelloController.java b/sample/src/main/java/com/raychatter/common/controller/HelloController.java index e3dfbe0..77369be 100755 --- a/sample/src/main/java/com/raychatter/common/controller/HelloController.java +++ b/sample/src/main/java/com/raychatter/common/controller/HelloController.java @@ -29,4 +29,10 @@ public class HelloController { throw new MyNegativeArraySizeException("oops"); } + @RequestMapping(value = "/unchecked", method = RequestMethod.GET) + @ResponseBody + public Object unchecked() throws Exception { + throw new NullPointerException("an npe"); + } + }