{"id":6440,"date":"2014-04-18T05:22:47","date_gmt":"2014-04-18T05:22:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/jsf-2-0-custom-exception-handler-collection-of-common-programming-errors\/"},"modified":"2014-04-18T05:22:47","modified_gmt":"2014-04-18T05:22:47","slug":"jsf-2-0-custom-exception-handler-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/jsf-2-0-custom-exception-handler-collection-of-common-programming-errors\/","title":{"rendered":"JSF 2.0 Custom Exception Handler-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m struggling fully understanding when\/how exceptions are thrown in JSF 2.0. I&#8217;ve looked for a solution longer than I care to admit. Ultimately, the goal I want to achieve is &#8220;handle&#8221; an unhandled exceptions. When an exception is thrown, I want to be able to capture information of interest about the exception, and email that to the appropriate site administrators. I&#8217;m forcing an error by throwing a new FacesException() in the constructor of one of my backing beans. I had this working great in JSF 1.1 using MyFaces implementation. I was able to get this working by wrapping the Default Lifecycle and simply overriding the execute() and render() methods. I followed this awesome post by Hanspeter to get that working:<\/p>\n<p>&#8220;http:\/\/insights2jsf.wordpress.com\/2009\/07\/20\/using-a-custom-lifecycle-implementation-to-handle-exceptions-in-jsf-1-2\/#comment-103&#8221;<\/p>\n<p>I am now undergoing a site upgrade to JSF 2.0 using Mojarra&#8217;s. And things work great still as long as the exception is thrown\/caught in the execute() method, however; the moment I enter the render(), the HttpServletResponse.isCommitted() equals true, and the phase is PhaseId RENDER_RESPONSE which of course means I can&#8217;t perform a redirect or forward. I don&#8217;t understand what has changed between JSF 1.1 and 2.0 in regards to when\/how the response is committed. As I indicated, I had this working perfectly in the 1.1 framework. After much searching I found that JSF 2.0 provides a great option for exception handling via a Custom ExceptionHandler. I followed Ed Burns&#8217; blog, Dealing Gracefully with ViewExpiredException in JSF2:<\/p>\n<p>&#8220;http:\/\/weblogs.java.net\/blog\/edburns\/archive\/2009\/09\/03\/dealing-gracefully-viewexpiredexception-jsf2&#8221;<\/p>\n<p>As Ed indicates there is always the web.xml way by defining the tag and what type of exception\/server error code and to what page one wants sent to for the error. This approach works great as long as I&#8217;m catching 404 errors. One interesting thing to note about that however, is if I force a 404 error by typing a non-exsitant URL like \/myApp\/9er the error handler works great, but as soon as I add &#8220;.xhtml&#8221; extension (i.e. \/myApp\/9er.xhtml) then the web.xml definition doesn&#8217;t handle it.<\/p>\n<p>One thing I noticed Ed was doing that I hadn&#8217;t tried was instead of trying to do a HttpServletRespone.sendRedirect(), he is utilizing the Navigationhandler.handleNavigation() to forward the user to the custom error page. Unfortunately, this method didn&#8217;t do anything different than what Faclets does with the error by default. Along with that of course, I was unable to do HttpServletResponse.sendRedirect() due to the same problems as mentioned above; response.isCommitted() equals true.<\/p>\n<p>I know this post is getting long so I will make a quick note about trying to use a PhaseListener for the same purposes. I used the following posts as a guide with this route still being unsuccessful:<\/p>\n<p>&#8220;http:\/\/ovaraksin.blogspot.com\/2010\/10\/global-handling-of-all-unchecked.html&#8221; &#8220;http:\/\/ovaraksin.blogspot.com\/2010\/10\/jsf-ajax-redirect-after-session-timeout.html&#8221;<\/p>\n<p>All and all I have the same issues as already mentioned. When this exception is thrown, the response is already in the committed phase, and I&#8217;m unable to redirect\/forward the user to a standard error page.<\/p>\n<p>I apologize for such a long post, I&#8217;m just trying to give as much information as possible to help eliminate ambiguity. Anyone have any ideas\/thoughts to a work around, and I&#8217;m curious what might be different between JSF 1.1 and 2.0 that would cause the response to be committed as soon as I enter the render() phase of the Lifecycle.<\/p>\n<p>Thanks a ton for any help with this!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m struggling fully understanding when\/how exceptions are thrown in JSF 2.0. I&#8217;ve looked for a solution longer than I care to admit. Ultimately, the goal I want to achieve is &#8220;handle&#8221; an unhandled exceptions. When an exception is thrown, I want to be able to capture information of interest about the exception, and email that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6440","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6440"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6440\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}