Tuesday, January 7, 2014

Error Handling in Faces Servlet.

==============================

Problem: Error Trace displayed on UI when there is exception in rendering the view.

==============================

 

 

[Reason why stack trace visible:] There is an entry in web.xml which says development = true. Because of this setting the error trace is seen on the UI.

If this param value is set to false then an empty page is shown, which is also not the correct behaviour.

 

  <context-param>

      <param-name>facelets.DEVELOPMENT</param-name>

      <param-value>true</param-value>

  </context-param>

 

Required behaviour is that the applications forwards the request to the correct error page.

 

 

==============================

Solution:

1.       Override the FaceletViewHandler.

2.       In the method handleRenderException, Forward to error.faces, which is the global error page for the application.

3.       Change the view-handler  in faces-config.xml to the overridden class.

==============================

                                                   

===============

Change in Faces-config.xml

===============

<view-handler>com.loginapp.extensions.LoginAppViewHandler</view-handler>

 

===============

New Class LoginAppViewHandler

 to handle the Exceptions when the view is generated.

===============

 

public class LoginAppViewHandler extends FaceletViewHandler{

      @Override

    protected void handleRenderException(FacesContext context, Exception ex) throws IOException, ELException,

        FacesException {

            logger.info("LoginAppViewHandler starts");

        try {

            logger.error("LoginAppViewHandler Printing the Error ", ex);

            HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

            request.getRequestDispatcher("/error.faces”).forward(request, response);

        } catch (Exception ioe) {

            logger.error("Could not process redirect to handle application error", ioe);

        }

        logger.info("LoginAppViewHandler ENDS");

    }

}

 

 

 



*****JuliusBaer Disclaimer***** This e-mail is for the intended recipient only and may contain confidential or privileged information. If you have received this e-mail by mistake, please contact us immediately and completely delete it (and any attachments) and do not forward it or inform any other person of its contents. If you send us messages by e-mail, we take this as your authorisation to correspond with you by e-mail, however, we reserve the right not to execute orders and instructions transmitted by e-mail at any time and without further explanation. If you do not wish to receive any further e-mail correspondence please let us know. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, amended, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. Neither the Julius Baer Group nor the sender accept liability for any errors or omissions in the content of this message which arise as a result of its e-mail transmission. Please note that all e-mail communications to and from the Julius Baer Group may be monitored. This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction.