@Test public void shouldHandleMalformedJSONException() throws Exception { mockStatic(RestResponse.class); when(RestResponse.error("unreadable", BAD_REQUEST)).thenReturn(new ResponseEntity<RestResponse>(BAD_REQUEST)); final ResponseEntity<RestResponse> response = baseController.handleException(new HttpMessageNotReadableException("unreadable")); assertThat(response.getStatusCode(), is(BAD_REQUEST)); }
@ExceptionHandler(Exception.class) public ResponseEntity<RestResponse> handleException(Exception ex) { if (ex instanceof AccessDeniedException) { return error(FORBIDDEN_EXCEPTION, FORBIDDEN); } if (ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof DataException) { return error(ex.getMessage(), BAD_REQUEST); } return error(UNEXPECTED_EXCEPTION, INTERNAL_SERVER_ERROR); }
BaseController { @ExceptionHandler(Exception.class) public ResponseEntity<RestResponse> handleException(Exception ex) { if (ex instanceof AccessDeniedException) { return error(FORBIDDEN_EXCEPTION, FORBIDDEN); } if (ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof DataException) { return error(ex.getMessage(), BAD_REQUEST); } return error(UNEXPECTED_EXCEPTION, INTERNAL_SERVER_ERROR); } }
BaseController { @ExceptionHandler(Exception.class) public ResponseEntity<RestResponse> handleException(Exception ex) { if (ex instanceof AccessDeniedException) { return error(FORBIDDEN_EXCEPTION, FORBIDDEN); } if (ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof DataException) { return error(ex.getMessage(), BAD_REQUEST); } return error(UNEXPECTED_EXCEPTION, INTERNAL_SERVER_ERROR); }  }
BaseController { @ExceptionHandler(Exception.class) public ResponseEntity<RestResponse> handleException(Exception ex) { if (ex instanceof AccessDeniedException) { return error(FORBIDDEN_EXCEPTION, FORBIDDEN); } if (ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof DataException) { return error(ex.getMessage(), BAD_REQUEST); } return error(UNEXPECTED_EXCEPTION, INTERNAL_SERVER_ERROR); }  @ExceptionHandler(Exception.class) ResponseEntity<RestResponse> handleException(Exception ex); Long loggedInUserId(Principal principal); }
BaseController { @ExceptionHandler(Exception.class) public ResponseEntity<RestResponse> handleException(Exception ex) { if (ex instanceof AccessDeniedException) { return error(FORBIDDEN_EXCEPTION, FORBIDDEN); } if (ex instanceof MissingServletRequestParameterException || ex instanceof HttpMessageNotReadableException || ex instanceof DataException) { return error(ex.getMessage(), BAD_REQUEST); } return error(UNEXPECTED_EXCEPTION, INTERNAL_SERVER_ERROR); }  @ExceptionHandler(Exception.class) ResponseEntity<RestResponse> handleException(Exception ex); Long loggedInUserId(Principal principal); static final String ACCEPT_JSON; static final String UNEXPECTED_EXCEPTION; static final String FORBIDDEN_EXCEPTION; }