/**
   * Builds the incident report model and view from the request that threw the exception
   *
   * @param request - the request
   * @param response - the response
   * @param handler - the current handler when the exception occurred
   * @param ex - the exception
   * @return the incident report model and view
   * @see
   *     org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
   */
  @Override
  public ModelAndView resolveException(
      HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);

    // log exception
    LOG.error(ex.getMessage(), ex);

    String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
    String incidentViewId = "";

    UifFormBase form = (UifFormBase) request.getAttribute(UifConstants.REQUEST_FORM);
    if (form instanceof DocumentFormBase) {
      if (((DocumentFormBase) form).getDocument() != null) {
        incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
      }
      incidentViewId = ((DocumentFormBase) form).getViewId();
    }

    if (GlobalVariables.getUifFormManager() != null) {
      GlobalVariables.getUifFormManager().removeSessionForm(form);
    }

    UserSession userSession =
        (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
    IncidentReportForm incidentReportForm = new IncidentReportForm();
    incidentReportForm.setSessionId(request.getSession().getId());

    // Set the post url map to the incident report controller and not
    // the one the exception occurred on
    String postUrl = request.getRequestURL().toString();
    postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
    incidentReportForm.setFormPostUrl(postUrl);

    incidentReportForm.setException(ex);
    incidentReportForm.setIncidentDocId(incidentDocId);
    incidentReportForm.setIncidentViewId(incidentViewId);
    incidentReportForm.setController(handler.getClass().toString());

    if (userSession != null) {
      incidentReportForm.setUserId(userSession.getPrincipalId());
      incidentReportForm.setUserName(userSession.getPrincipalName());
      incidentReportForm.setUserEmail(userSession.getPerson().getEmailAddress());
    }

    incidentReportForm.setDevMode(!KRADUtils.isProductionEnvironment());
    incidentReportForm.setViewId("Uif-IncidentReportView");

    if (form != null) {
      incidentReportForm.setAjaxRequest(form.isAjaxRequest());
    } else {
      String ajaxRequestParm = request.getParameter(UifParameters.AJAX_REQUEST);
      if (StringUtils.isNotBlank(ajaxRequestParm)) {
        incidentReportForm.setAjaxRequest(Boolean.parseBoolean(ajaxRequestParm));
      }
    }

    // Set the view object
    incidentReportForm.setView(getViewService().getViewById("Uif-IncidentReportView"));

    // Set the ajax return type
    incidentReportForm.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey());

    ModelAndView modelAndView = getModelAndViewService().getModelAndView(incidentReportForm, "");
    try {
      getModelAndViewService().prepareView(request, modelAndView);
    } catch (Exception e) {
      LOG.error("An error stopped the incident form from loading", e);
    }

    return modelAndView;
  }