[jboss-user] [JBoss Seam] - Re: Newbie Problem with Exception handling and pages.xml

sheadley3228 do-not-reply at jboss.com
Wed Dec 26 13:46:26 EST 2007


Seams like have am now able to transfer to my error.xhtml page, but I have another problem. I don't have access to the xception message or anything dealing with the exception. I have tried 2 approaches:  

1. Create an Exception Listener:


  | import java.io.PrintWriter;
  | import java.io.StringWriter;
  | import java.util.Iterator;
  | import java.util.List;
  | import java.util.Map;
  | 
  | import javax.faces.event.PhaseEvent;
  | import javax.faces.event.PhaseId;
  | import javax.faces.event.PhaseListener;
  | 
  | import org.jboss.seam.annotations.Logger;
  | import org.jboss.seam.log.Log;
  | import javax.faces.application.FacesMessage;
  | import javax.faces.component.UIViewRoot;
  | import javax.faces.context.ExternalContext;
  | import javax.faces.context.FacesContext;
  | import javax.servlet.ServletContext;
  | import javax.servlet.ServletException;
  | import javax.servlet.http.HttpServletRequest;
  | import javax.servlet.http.HttpServletResponse;
  | 
  | public class ExceptionListener implements PhaseListener{
  | 
  | @Logger private Log log;
  | static private FacesContext _instance = null;
  | 	
  | 	
  | 	static public FacesContext getContext() {
  | 	      return _instance;
  | 	 }
  | 	
  | 	
  | 	public void beforePhase(PhaseEvent event) {
  | 		System.out.println("ExceptionListener BeforePhase: " + event.getPhaseId());
  | 		String viewId = "/document/error.jsp";
  | 		
  | 		PhaseId phase = event.getPhaseId();  
  | 		if (phase == PhaseId.INVOKE_APPLICATION) { 
  | 			
  | 		    try {
  | 		    	set_instance(event.getFacesContext());
  | 		    	
  | 		    	
  | 		       	if (getContext() != null && getContext().getExternalContext() != null) 
  | 		       	{
  | 		       		System.out.println("Analyzing context");
  | 				    // Any errors?
  | 		       		String tracing = getStackTrace();
  | 		       		
  | 		       		System.out.println("trace found "+ tracing);
  | 		       		
  | 		       		// has information so place in message
  | 		       		if(tracing.length() > 0)
  | 		       		{	
  | 		       			UIViewRoot view = getContext().getApplication().getViewHandler().
  | 		       									createView(getContext(), viewId);
  | 		       			view.setViewId(viewId); 
  | 		       			getContext().setViewRoot(view); 
  | 		       			getContext().renderResponse();
  | 		       		}
  | 		       		else
  | 		       			return;
  | 		       	}	
  | 		    }
  | 		    catch (Exception ex) {  
  | 		       	log.error(ex.getMessage());  
  | 		       	ex.printStackTrace();  
  | 		    }
  | 		}    
  |     }
  | 
  |     public void afterPhase(PhaseEvent event) {
  |     	System.out.println("ExceptionListener AfterPhase: " + event.getPhaseId());
  | 		String viewId = "/document/error.jsp";
  | 		
  | 		PhaseId phase = event.getPhaseId();  
  | 		if (phase == PhaseId.INVOKE_APPLICATION) { 
  | 			
  | 		    try {
  | 		    	System.out.println("Setting instance");
  | 		    	set_instance(event.getFacesContext());
  | 		    	System.out.println("getContext()");
  | 		       	if (getContext() != null && getContext().getExternalContext() != null) 
  | 		       	{
  | 		       		System.out.println("Analyzing context");
  | 				    // Any errors?
  | 		       		String tracing = getStackTrace();
  | 		       		
  | 		       		System.out.println("[[[[trace found "+ tracing +" ]]]]");
  | 		       		
  | 		       		// has information so place in message
  | 		       		if(tracing.length() > 0)
  | 		       		{	
  | 		       			UIViewRoot view = getContext().getApplication().getViewHandler().
  | 		       									createView(getContext(), viewId);
  | 		       			view.setViewId(viewId); 
  | 		       			getContext().setViewRoot(view); 
  | 		       			getContext().renderResponse();
  | 		       		}
  | 		       		else
  | 		       			return;
  | 		       	}	
  | 		    }
  | 		    catch (Exception ex) {  
  | 		       	log.error(ex.getMessage());  
  | 		       	ex.printStackTrace();  
  | 		    }
  | 		}
  |     }
  | 
  |     public PhaseId getPhaseId() {
  |         return PhaseId.INVOKE_APPLICATION;
  |     }
  |     
  |     public String getStackTrace() {
  |     	System.out.println(">>>>getStackTrace()");
  |     	Object codeObj = null, messageObj = null, typeObj = null;
  |     	
  |     	System.out.println("Get Request Map");
  |     	ExternalContext xctx = getContext().getExternalContext();
  | 		Object context = xctx.getContext();
  | 		//
  | 		if(context instanceof ServletContext){			
  | 			HttpServletRequest request = (HttpServletRequest)getContext().getExternalContext().getRequest();
  | 			codeObj = request.getAttribute("javax.servlet.error.status_code");
  |             messageObj = request.getAttribute("javax.servlet.error.message");
  |             typeObj = request.getAttribute("javax.servlet.error.exception_type");
  | 		}
  |         
  |         System.out.println("Get javax.servlet.error.exception");
  |         
  |         	
  |         return messageObj.toString();
  |        
  |     }
  |     
  |     private void fillStackTrace(Throwable ex, PrintWriter pw) {
  |         if (null == ex) {
  |             return;
  |         }
  | 
  |         ex.printStackTrace(pw);
  | 
  |         if (ex instanceof ServletException) {
  |             Throwable cause = ((ServletException) ex).getRootCause();
  | 
  |             if (null != cause) {
  |                 pw.println("Root Cause:");
  |                 fillStackTrace(cause, pw);
  |             }
  |         } else {
  |             Throwable cause = ex.getCause();
  | 
  |             if (null != cause) {
  |                 pw.println("Cause:");
  |                 fillStackTrace(cause, pw);
  |             }
  |         }
  |     }
  | 
  | 
  | 	public static void set_instance(FacesContext _instance) {
  | 		ExceptionListener._instance = _instance;
  | 	}
  | }
  | 

the other method that I used was to create an exception bean:


  | @Stateful
  | @Name("errorDisplayBean")
  | @Scope(ScopeType.APPLICATION)
  | @Synchronized
  | @Startup 
  | public class ErrorDisplayBean implements ErrorDisplay{
  | 	public String getStackTrace() {
  | 		System.out.println(">>>>ErrorDisplayBean");
  | 		FacesContext context = FacesContext.getCurrentInstance();
  | 		Map request = context.getExternalContext().getRequestMap();
  | 		Throwable ex = (Throwable) request.get("javax.servlet.error.exception");
  | 		StringWriter sw = new StringWriter();
  | 		PrintWriter pw = new PrintWriter(sw);
  | 		fillStackTrace(ex, pw);
  | 		return sw.toString();
  | 	}
  |  
  | 	private static void fillStackTrace(Throwable t, PrintWriter w) {
  | 		if (t == null)
  | 			return;
  | 		t.printStackTrace(w);
  | 		if (t instanceof ServletException) {
  | 			Throwable cause = ((ServletException) t).getRootCause();
  | 			if (cause != null) {
  | 				w.println("Root cause:");
  | 				fillStackTrace(cause, w);
  | 			}
  | 		} else if (t instanceof SQLException) {
  | 			Throwable cause = ((SQLException) t).getNextException();
  | 			if (cause != null) {
  | 				w.println("Next exception:");
  | 				fillStackTrace(cause, w);
  | 			}
  | 		} else {
  | 			Throwable cause = t.getCause();
  | 			if (cause != null) {
  | 				w.println("Cause:");
  | 				fillStackTrace(cause, w);
  | 			}
  | 		}
  | 	}
  | 	
  | 	@Destroy @Remove
  |     public void destroy() {}
  | }
  | 


None of these methods returned anything concerning the exception that was thrown. In both cases I had seam debug mode and facelets development turned off.

Thanks for your prompt help

Regards

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4115534#4115534

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4115534



More information about the jboss-user mailing list