3 Replies Latest reply on Feb 11, 2010 4:58 PM by zjda

    Handle exception in action methods

    zjda

      Hi, I want a general way to handle exceptions in the action/actionListener methods.  Here is the action method:

       

          public String refreshObjectAction() throws Exception {
              try {
                  throw new Exception("test error message!");
              } catch (Exception e) {
                  FacesContext fc = FacesContext.getCurrentInstance();
                  HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
                 
                  response.sendError(Constants.SC_INTERNAL_SERVER_ERROR, e.getMessage());
                  response.setStatus(Constants.SC_INTERNAL_SERVER_ERROR);
                  fc.addMessage("errorPanel", new FacesMessage(FacesMessage.SEVERITY_FATAL, e.getMessage(), e.toString()));
                  fc.responseComplete();       
              }
             
             return null;
          }

       

      I try to use rich:modalPanel to display the error message from the exception:

       

      <a4j:outputPanel ajaxRendered="#{not (facesContext.maximumSeverity==null)}">
          <rich:modalPanel id="errorPanel" zindex="2000" resizeable="false" minWidth="100"
              minHeight="40" showWhenRendered="true">
              <f:facet name="header">Error</f:facet>
              <f:facet name="controls">
                  <h:graphicImage value="/images/close.gif" style="cursor:pointer" onclick="Richfaces.hideModalPanel('errorPanel');" />
              </f:facet>
              <rich:panel styleClass="main_error_panel">
                  <h:panelGrid columns="2" width="100%" columnClasses="image_col, message_col"
                      style="margin:0px; padding:0px; border:5px">
                      <h:graphicImage value="/images/bigError.gif" />       
                      <rich:messages/>
                  </h:panelGrid>
                  <a4j:commandButton value="OK" styleClass="dialog_button"
                      onclick="Richfaces.hideModalPanel('errorPanel'); return false;">
                  </a4j:commandButton>
              </rich:panel>
          </rich:modalPanel>
      </a4j:outputPanel>

      However, the dialog does not show up after the action method is called.  If I add the following code:

       

                  A4J.AJAX.onError = function(req, status, message)
                      Richfaces.showModalPanel('errorPanel');
                  }

      The dialog is displayed, but there is no message shown in the dialog.  I want this dialog to display exception errors only, not the validation messages.  Any suggestion will be appreciated.

       

      -ZJ

        • 1. Re: Handle exception in action methods
          zjda

          Hi, I play with the issue and I am able to display the message in the dialog with the action method:

           

              public String refreshObjectAction() throws Exception {
                  try {
                      throw new Exception("test error message!");
                  } catch (Exception e) {
                      FacesContext.getCurrentInstance().addMessage(null,
                          new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getStackTrace()[0].toString()));
                  }
                 
                  return null;
              }

           

           

           

          and the dialog code:

           

          <a4j:outputPanel ajaxRendered="true">
              <rich:modalPanel id="errorPanel" zindex="2000" resizeable="false" minWidth="100"
                  minHeight="40" showWhenRendered="true">
                  <f:facet name="header">Error</f:facet>
                  <f:facet name="controls">
                      <h:graphicImage value="/images/close.gif" style="cursor:pointer" onclick="Richfaces.hideModalPanel('errorPanel');" />
                  </f:facet>
                  <rich:panel styleClass="main_error_panel">
                      <h:panelGrid columns="2" width="100%" columnClasses="image_col, message_col"
                          style="margin:0px; padding:0px; border:5px">
                          <h:graphicImage value="/images/bigError.gif" />       
                          <rich:messages/>
                      </h:panelGrid>
                      <a4j:commandButton value="OK" styleClass="dialog_button"
                          onclick="Richfaces.hideModalPanel('errorPanel'); return false;">
                      </a4j:commandButton>
                  </rich:panel>
              </rich:modalPanel>
          </a4j:outputPanel>

           

           

          As expected the dialog is displayed for every request.  If I replace the first line of the dialog code with

           

          <a4j:outputPanel ajaxRendered="#{facesContext.maximumSeverity != null}">

           

          The dialog does not show up at all.  Could someone told me why the condition "#{facesContext.maximumSeverity != null}" does not work and how to fix it?

           

          Thanks,

          -ZJ

          • 2. Re: Handle exception in action methods
            zjda

            Hi.  I still struggle with the issue and here is my current dialog code:

             

            <a4j:outputPanel ajaxRendered="true" >
                <rich:modalPanel id="errorPanel" zindex="2000" resizeable="false" minWidth="100"
                    minHeight="40" showWhenRendered="true">
                    <f:facet name="header">Error</f:facet>
                    <f:facet name="controls">
                        <h:graphicImage value="/images/close.gif" style="cursor:pointer" onclick="Richfaces.hideModalPanel('errorPanel');" />
                    </f:facet>
                        Is message queue empty? "#{empty facesMessages.currentGlobalMessages}"
                        <br/>
                        <rich:messages globalOnly="true">
                        </rich:messages>
                        <br/>
                        <a4j:commandButton value="OK" styleClass="dialog_button"
                            onclick="Richfaces.hideModalPanel('errorPanel'); return false;">
                        </a4j:commandButton>
                </rich:modalPanel>
            </a4j:outputPanel>

             

            The #{empty facesMessages.currentGlobalMessages} always returns "true" while <rich:messages> displays the message set in the action method.   Could someone shed light on it?

             

            Thanks,

            -ZJ

            • 3. Re: Handle exception in action methods
              zjda

              Finally I solved the problem and I want to share my solution here. The dialog looks like

               

              <a4j:outputPanel ajaxRendered="true" >
                  <rich:modalPanel id="errorPanel" zindex="2000" resizeable="false"
                      autosized="true" showWhenRendered="#{apiException}">

              ...

               

              and method:

               

                      try {
                          throw new Exception("test error message!");
                      } catch (Exception e) {
                          FacesContext fc = FacesContext.getCurrentInstance();
                          HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
                          request.setAttribute("apiException", "true");
                          fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
                      }

               

              cheers,
              -ZJ