HodViewController extends ViewController<HodViewRequest, ResourceName, HodErrorException> { @SuppressWarnings("TypeMayBeWeakened") @ExceptionHandler public ModelAndView handleHodErrorException( final HodErrorException e, final HttpServletRequest request, final HttpServletResponse response ) { response.reset(); log.error("HodErrorException thrown while viewing document", e); final String errorKey = HOD_ERROR_MESSAGE_CODE_PREFIX + e.getErrorCode(); String hodErrorMessage; try { hodErrorMessage = controllerUtils.getMessage(errorKey, null); } catch (final NoSuchMessageException ignored) { hodErrorMessage = controllerUtils.getMessage(HOD_ERROR_MESSAGE_CODE_UNKNOWN, null); } final int errorCode = e.isServerError() ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : HttpServletResponse.SC_BAD_REQUEST; final String subMessageCode; final Object[] subMessageArgs; if (hodErrorMessage != null) { subMessageCode = HOD_ERROR_MESSAGE_CODE_SUB; subMessageArgs = new String[]{hodErrorMessage}; } else { subMessageCode = HOD_ERROR_MESSAGE_CODE_SUB_NULL; subMessageArgs = null; } response.setStatus(errorCode); return controllerUtils.buildErrorModelAndView(new ErrorModelAndViewInfo.Builder() .setRequest(request) .setMainMessageCode(HOD_ERROR_MESSAGE_CODE_MAIN) .setSubMessageCode(subMessageCode) .setSubMessageArguments(subMessageArgs) .setStatusCode(errorCode) .setContactSupport(true) .setException(e) .build()); } @SuppressWarnings("TypeMayBeWeakened") @Autowired  HodViewController(final HodViewServerService viewServerService,
                             final ObjectFactory<HodViewRequestBuilder> viewRequestBuilderFactory,
                             final ControllerUtils controllerUtils); @SuppressWarnings("TypeMayBeWeakened") @ExceptionHandler ModelAndView handleHodErrorException(
            final HodErrorException e,
            final HttpServletRequest request,
            final HttpServletResponse response
    ); @SuppressWarnings("TypeMayBeWeakened") @ExceptionHandler ModelAndView hodAuthenticationFailedException(
            final HodAuthenticationFailedException e,
            final HttpServletRequest request,
            final HttpServletResponse response
    ); @SuppressWarnings("TypeMayBeWeakened") @ExceptionHandler @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) ModelAndView handleGeneralException(
            final Exception e,
            final HttpServletRequest request,
            final ServletResponse response
    );  }
@Test public void handleKnownHodErrorException() { when(controllerUtils.getMessage(anyString(), any(Object[].class))).thenReturn("Some known error message"); assertNotNull(viewController.handleHodErrorException(new HodErrorException(new HodError.Builder().build(), 400), new MockHttpServletRequest(), new MockHttpServletResponse())); verify(controllerUtils).buildErrorModelAndView(any(ErrorModelAndViewInfo.class)); }
@Test public void handleUnknownHodErrorException() { when(controllerUtils.getMessage(anyString(), any(Object[].class))).thenThrow(new NoSuchMessageException("")).thenReturn(null); assertNotNull(viewController.handleHodErrorException(new HodErrorException(new HodError.Builder().build(), 400), new MockHttpServletRequest(), new MockHttpServletResponse())); verify(controllerUtils).buildErrorModelAndView(any(ErrorModelAndViewInfo.class)); }