Added a default behavior for the exceptions that are not checked
This commit is contained in:
@@ -23,9 +23,12 @@ public class AnnotationHandler implements HandlerExceptionResolver {
|
|||||||
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception thrownException) {
|
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception thrownException) {
|
||||||
final ExceptionHandler annotation = getAnnotationFrom(thrownException);
|
final ExceptionHandler annotation = getAnnotationFrom(thrownException);
|
||||||
|
|
||||||
if (annotation == null) return new ModelAndView();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (annotation == null) {
|
||||||
|
thrownException.printStackTrace();
|
||||||
|
return respondWithDefault(thrownException, response);
|
||||||
|
}
|
||||||
|
|
||||||
return handleException(annotation, thrownException, response);
|
return handleException(annotation, thrownException, response);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// potentially something went wrong in response itself
|
// potentially something went wrong in response itself
|
||||||
@@ -43,10 +46,16 @@ public class AnnotationHandler implements HandlerExceptionResolver {
|
|||||||
final String message = formatMessage(thrownException);
|
final String message = formatMessage(thrownException);
|
||||||
response.getWriter().write(message);
|
response.getWriter().write(message);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
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.setContentType(MediaType.APPLICATION_XML_VALUE);
|
||||||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||||
response.getWriter().write(formatDefaultMessage(thrownException));
|
response.getWriter().write(formatDefaultMessage(thrownException));
|
||||||
}
|
|
||||||
|
|
||||||
return new ModelAndView();
|
return new ModelAndView();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,4 +29,10 @@ public class HelloController {
|
|||||||
throw new MyNegativeArraySizeException("oops");
|
throw new MyNegativeArraySizeException("oops");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/unchecked", method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public Object unchecked() throws Exception {
|
||||||
|
throw new NullPointerException("an npe");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user