Exception Handling and Logging
walterjwhite Feb 22, 2009 7:04 PMHi all,
I have a exception observer class which is supposed to catch all exception events. When it catches such an event, it notifies administrators of the exception. My question is, is it possible to have access to the URL or Identity if the exception occurred as a direct result from page navigation? That would make my life a little bit easier when tracking down bugs.
This is the class that I have setup to be an interceptor or listener rather. I am thinking that I might be able to do something with the context to get the page and possibly the identity of the logged in user from the session context.
package com.walterjwhite.interceptor; import java.util.Collection; import java.util.Date; import javax.ejb.Stateless; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Observer; import org.jboss.seam.annotations.Out; import org.jboss.seam.contexts.Contexts; import org.jboss.seam.faces.Renderer; import org.jboss.seam.framework.HibernateEntityQuery; import org.jboss.seam.log.Log; import com.walterjwhite.enumeration.EmailType; import com.walterjwhite.model.Member; @Stateless @Name("asynchronousExceptionInterceptor") public class AsynchronousExceptionInterceptor { @Logger protected transient Log log; @In protected Exception cause; @In protected HibernateEntityQuery<Member> administratorQuery; @In protected Renderer renderer; @Out protected Collection<Member> administrators; @Out protected String subject; @Out protected Date dateOccurred; @Out protected String stackTrace; @Observer( { "org.jboss.seam.async.asynchronousExceptionHandler", "org.jboss.seam.exceptionHandled", "org.jboss.seam.exceptionNotHandled" }) public void onError() { log.info("notifying administrators of exception."); administrators = administratorQuery.getResultList(); dateOccurred = new Date(); Contexts.getSessionContext(); subject = cause.getMessage(); StringBuilder buffer = new StringBuilder(); for (StackTraceElement element : cause.getStackTrace()) buffer.append(element.toString() + "\n"); stackTrace = buffer.toString(); buffer = null; renderer.render(EmailType.EXCEPTION.getPath()); log.error("captured exception", cause); log.info("notified administrators of exception."); } }
Is this possible?
I do not have any integration tests running yet, but I'm trying to understand from a high level how things can work.
Thanks,
Walter