From ec33df29e7e9077cca64ad65bb5db51e7984f03b Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 2 May 2002 07:20:04 +0000 Subject: [PATCH] Listen for and report exceptions thrown while executing methods in Velocity. git-svn-id: https://samskivert.googlecode.com/svn/trunk@724 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../velocity/DispatcherServlet.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java index eae4ed9b..474a50cd 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.16 2002/05/02 05:30:31 mdb Exp $ +// $Id: DispatcherServlet.java,v 1.17 2002/05/02 07:20:04 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -38,6 +38,9 @@ import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.RuntimeSingleton; import org.apache.velocity.servlet.VelocityServlet; +import org.apache.velocity.app.event.EventCartridge; +import org.apache.velocity.app.event.MethodExceptionEventHandler; + import com.samskivert.Log; import com.samskivert.servlet.MessageManager; @@ -149,6 +152,7 @@ import com.samskivert.util.StringUtil; * @see ExceptionMap */ public class DispatcherServlet extends VelocityServlet + implements MethodExceptionEventHandler { /** * Initialize ourselves and our application. @@ -233,6 +237,14 @@ public class DispatcherServlet extends VelocityServlet InvocationContext ictx = (InvocationContext)ctx; String errmsg = null; + // listen for exceptions so that we can report them + EventCartridge ec = ictx.getEventCartridge(); + if (ec == null) { + ec = new EventCartridge(); + ec.attachToContext(ictx); + ec.addEventHandler(this); + } + // obtain the siteid for this request and stuff that into the // context SiteIdentifier ident = _app.getSiteIdentifier(); @@ -301,6 +313,19 @@ public class DispatcherServlet extends VelocityServlet return tmpl; } + /** + * Called when a method throws an exception during template + * evaluation. + */ + public Object methodException (Class clazz, String method, Exception e) + throws Exception + { + Log.warning("Exception [class=" + clazz.getName() + + ", method=" + method + "]."); + Log.logStackTrace(e); + return ""; + } + /** * Returns the reference to the application that is handling this * request.