private ResponseEntity<Object> testException(Exception ex) { try { ResponseEntity<Object> responseEntity = this.exceptionHandlerSupport.handleException(ex, this.request); // SPR-9653 if (HttpStatus.INTERNAL_SERVER_ERROR.equals(responseEntity.getStatusCode())) { assertSame(ex, this.servletRequest.getAttribute("javax.servlet.error.exception")); } this.defaultExceptionResolver.resolveException(this.servletRequest, this.servletResponse, null, ex); assertEquals(this.servletResponse.getStatus(), responseEntity.getStatusCode().value()); return responseEntity; } catch (Exception ex2) { throw new IllegalStateException("handleException threw exception", ex2); } }
@Test public void handleConversionNotSupportedException() throws Exception { ConversionNotSupportedException ex = new ConversionNotSupportedException(new Object(), String.class, new Exception()); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 500, response.getStatus()); // SPR-9653 assertSame(ex, request.getAttribute("javax.servlet.error.exception")); }
@Test public void handleBindException() throws Exception { BindException ex = new BindException(new Object(), "name"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test public void handleMissingPathVariable() throws NoSuchMethodException { Method method = getClass().getMethod("handle", String.class); MethodParameter parameter = new MethodParameter(method, 0); MissingPathVariableException ex = new MissingPathVariableException("foo", parameter); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 500, response.getStatus()); assertEquals("Missing URI template variable 'foo' for method parameter of type String", response.getErrorMessage()); }
@Test public void handleMissingServletRequestPartException() throws Exception { MissingServletRequestPartException ex = new MissingServletRequestPartException("name"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); assertTrue(response.getErrorMessage().contains("request part")); assertTrue(response.getErrorMessage().contains("name")); assertTrue(response.getErrorMessage().contains("not present")); }
@Test public void handleMethodArgumentNotValid() throws Exception { BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean"); errors.rejectValue("name", "invalid"); MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0); MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test public void handleServletRequestBindingException() { String message = "Missing required value - header, cookie, or pathvar"; ServletRequestBindingException ex = new ServletRequestBindingException(message); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test public void handleHttpMessageNotWritable() { HttpMessageNotWritableException ex = new HttpMessageNotWritableException("foo"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 500, response.getStatus()); }
@Test public void handleTypeMismatch() { TypeMismatchException ex = new TypeMismatchException("foo", String.class); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test public void handleHttpMessageNotReadable() { HttpMessageNotReadableException ex = new HttpMessageNotReadableException("foo"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }
@Test // SPR-14669 public void handleAsyncRequestTimeoutException() throws Exception { Exception ex = new AsyncRequestTimeoutException(); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 503, response.getStatus()); }
@Test public void handleMissingServletRequestParameter() { MissingServletRequestParameterException ex = new MissingServletRequestParameterException("foo", "bar"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); assertEquals("Required bar parameter 'foo' is not present", response.getErrorMessage()); }
@Test public void handleHttpMediaTypeNotSupported() { HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException(new MediaType("text", "plain"), Collections.singletonList(new MediaType("application", "pdf"))); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 415, response.getStatus()); assertEquals("Invalid Accept header", "application/pdf", response.getHeader("Accept")); }
@Test public void handleHttpRequestMethodNotSupported() { HttpRequestMethodNotSupportedException ex = new HttpRequestMethodNotSupportedException("GET", new String[]{"POST", "PUT"}); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 405, response.getStatus()); assertEquals("Invalid Allow header", "POST, PUT", response.getHeader("Allow")); }
@Test public void handleNoHandlerFoundException() throws Exception { ServletServerHttpRequest req = new ServletServerHttpRequest( new MockHttpServletRequest("GET","/resource")); NoHandlerFoundException ex = new NoHandlerFoundException(req.getMethod().name(), req.getServletRequest().getRequestURI(),req.getHeaders()); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 404, response.getStatus()); }
@ExceptionHandler @ResponseBody public Object handleException(HttpServletRequest request, final HttpServletResponse response, Object handler, Exception ex) throws IOException { logger.debug("Error in controller", ex); final AtomicInteger errorCode = new AtomicInteger(500); if(ex instanceof ComponentValidationException || ex instanceof IllegalArgumentException) { errorCode.set(422); } else if(ex instanceof AccessDeniedException) { errorCode.set(HttpServletResponse.SC_FORBIDDEN); } else { // Using DefaultHandlerExceptionResolver to map standard Spring exception // like NoSuchRequestHandlingMethodException (HTTP 404) etc... HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response) { public void sendError(int sc, String msg) throws IOException { errorCode.set(sc); } public void sendError(int sc) throws IOException { errorCode.set(sc); } }; new DefaultHandlerExceptionResolver().resolveException(request, responseWrapper, handler, ex); } return ResponseEntity.status(errorCode.get()).body(new ErrorResponse(ex)); }