10 Replies Latest reply on Jun 1, 2011 5:39 AM by Sebastian Sachtleben

    Problems with redirect after catching Exception

    Melanie Siekmöller Newbie

      Hi all,


      I have some problem with Seam Catch, maybe someone can help me...
      The following ExceptionHandler handles NullPointerExceptions:




      @HandlesExceptions
      public class ExceptionHandler {
           
           public void handleNullPointerException(
                @Handles(during = TraversalMode.DEPTH_FIRST, precedence = Precedence.HIGH) CaughtException<NullPointerException> evt,
                ExternalContext ec, HttpServletResponse response) {
      
                boolean comitted = response.isCommitted();
      
                if (!evt.isMarkedHandled() && !comitted) {
                     try {
                          response.sendRedirect(ec.getRequestContextPath() + "/error.seam");
                     } catch (IOException e) {
                          e.printStackTrace();
                     }
                }
      
           }
           
      }



      On a test.xhtml page I have the following output:


      <h:outputText value="#{requestTestBean}" /> 



      and in the RequestTestBean I force an exception in the @PostConstruct method


      @PostConstruct
      public void init() {
           Object x = null;
           x.toString();
      }



      The redirect to my error page works fine for this little example but when I add considerable more content on the page (like a menu with many entries and icons) the redirect doesn't work because the response is already commited in the exceptionHandler method.
      I also tried to redirect to the error page via navigationHandler but the same problems occured.


      Maybe this is not a Seam Catch problem and maybe I'm missing something or doesn't understand the JSF-Phases (RENDER_RESPONSE) correctly...
      Has someone an idea why this happens? Is the ExceptionHandler the correct place for redirects?


      Thanks a lot!
      MK

        • 1. Re: Problems with redirect after catching Exception
          Jason Porter Master

          Could you please inject the FacesContext (or getCurrent) and tell us which phase you're in when you hit the catch handler?

          • 2. Re: Problems with redirect after catching Exception
            Melanie Siekmöller Newbie

            It just happens when I'm in the RENDER_RESPONE Phase.
            When I'm forcing the error in another Phase (e.g. with a h:comandButton in INVOKE_APPLICATION) it works fine.

            • 3. Re: Problems with redirect after catching Exception
              Jason Porter Master

              This may be a JSF issue as it seems like data has already been written to the response, not sure if there's a great way of doing this if an error occurs during the RENDER_RESPONSE phase. We may need a more advanced JSF guru to comment. You can also watch http://seamframework.org/Community/SeamCatchViewExpiredExceptionAndRedirect as that is a similar issue.

              • 5. Re: Problems with redirect after catching Exception
                marx3 Apprentice

                If exception happens in ajax action with partial page update, redirect will not work too.

                • 6. Re: Problems with redirect after catching Exception
                  Sebastian Sachtleben Novice

                  I have another question about Seam Catch since I missed Jason in the last three days in irc. I catch several exceptions and redirect to my startpage which works fine. But I have still two problems with that.


                  1) The full stacktrace will be posted in server logs - I see that the handler catch the exception after that log message. Is it possible to prevent that logging? If 100 users cause that exception in 5 minutes the logs will be explode...


                  2) How to add messages after the redirect? I have tried to set a message below but it doesnt get displayed?!?


                  Thanks :)


                       
                       @Inject
                       private FacesContext facesContext;
                       
                       @Inject
                       private Messages messages;
                       
                       @Inject @ContextPath
                       private String contextPath;     
                       
                       void handleForumNotFoundException(@Handles(precedence = 100) CaughtException<ForumNotFoundException> event, Logger log) {
                            String redirectTo = contextPath + "/forums.html";
                            log.info("Handle ForumNotFoundException -> redirect to: " + redirectTo);
                            event.markHandled();
                            try {
                                 facesContext.getExternalContext().redirect(redirectTo);
                            } catch (IOException e) {
                                 e.printStackTrace();
                            }
                            messages.error(new BundleKey("messages", "exceptionhandler.error.ForumNotFoundException")).defaults("Board forum not found");          
                       }



                  • 7. Re: Problems with redirect after catching Exception
                    Jason Porter Master

                    Marek Nazarko wrote on May 24, 2011 09:10:


                    If exception happens in ajax action with partial page update, redirect will not work too.


                    That's probably a JSF issue.

                    • 8. Re: Problems with redirect after catching Exception
                      Jason Porter Master

                      Sebastian Sachtleben wrote on May 24, 2011 09:38:


                      I have another question about Seam Catch since I missed Jason in the last three days in irc. I catch several exceptions and redirect to my startpage which works fine. But I have still two problems with that.

                      1) The full stacktrace will be posted in server logs - I see that the handler catch the exception after that log message. Is it possible to prevent that logging? If 100 users cause that exception in 5 minutes the logs will be explode...


                      Yeah, that's a Mojarra issue, they fixed it (set it to trace level instead of info or debug) in the 2.1 release. Very annoying I agree.



                      2) How to add messages after the redirect? I have tried to set a message below but it doesnt get displayed?!?

                      Thanks :)

                             
                              @Inject
                              private FacesContext facesContext;
                              
                              @Inject
                              private Messages messages;
                              
                              @Inject @ContextPath
                              private String contextPath;     
                              
                              void handleForumNotFoundException(@Handles(precedence = 100) CaughtException<ForumNotFoundException> event, Logger log) {
                                      String redirectTo = contextPath + "/forums.html";
                                      log.info("Handle ForumNotFoundException -> redirect to: " + redirectTo);
                                      event.markHandled();
                                      try {
                                              facesContext.getExternalContext().redirect(redirectTo);
                                      } catch (IOException e) {
                                              e.printStackTrace();
                                      }
                                      messages.error(new BundleKey("messages", "exceptionhandler.error.ForumNotFoundException")).defaults("Board forum not found");           
                              }




                      I believe this is due to the phase in which the handler is being executed. I've been very disillusioned in the exception handling available in JSF. An option (and would really help us figure out what's going on here) would be to do this with standard JSF2 exception handling and see what the outcome is. If it's the same, then it's something that will need to be changed in JSF, otherwise we'll have to look at our integration and see what's going on. If you don't mind doing that Sebastian, it would be a great help.

                      • 9. Re: Problems with redirect after catching Exception
                        Sebastian Sachtleben Novice

                        I think I'm also in render phase since my view invokes somewhere an method which cause the exception if the forum is not found in database depending on the url parameter.


                        Do u mean this?


                        I saw someone is using this inside of the exception handler:


                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error occured"));



                        If you mean that I could try it out.

                        • 10. Re: Problems with redirect after catching Exception
                          Sebastian Sachtleben Novice

                          I have tried to add a FacesMessage with the normal jsf 2 error catching but its gone also after redirect. If I use s:viewAction with pretty id as return string the redirect works with the facesmessage but I cant get it to work with the error catching at all.