spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.63k stars 38.13k forks source link

Improved handling of exceptions thrown in Velocity pages [SPR-1448] #6148

Closed spring-projects-issues closed 12 years ago

spring-projects-issues commented 19 years ago

Noa Resare opened SPR-1448 and commented

velocity templates has the ability to call methods when rendered. Sometimes the results of such method invocations is an exception. These exceptions are propagated as org.apache.velocity.exception.MethodInvocationException instances through the call stack. There are two problems with the current out of the box behaviour:

1) Velocity aims to be jvm1.3 compatible, so the generic exception nesting mechanism in java1.4 is not used for the causing exception. Instead the method getWrappedThrowable() is used. This effectively makes the causing exception disappear from the stack trace in most cases.

2) The name of the View that was executing when the exception was thrown is not shown.

To solve this problem I have subclassed the VelocityView, adding this method that handles these two problems:

@Override
public void render(Map model, HttpServletRequest req, 
        HttpServletResponse resp)
    throws Exception
{
    try {
        super.render(model, req, resp);
    } catch (MethodInvocationException e) {
        String s = String.format(
                "MethodInvocationException when rendering view '%s': %s",
                getBeanName(), e.getMessage());
        throw new RuntimeException(s, e.getWrappedThrowable());
    }
}

It hink it would be a valuable improvement if this functionality was incorporated in for example VelocityView.doRender()

Thanks for a great product. After being stuck in ejb hell for too long, spring feels like heaven :)


Affects: 1.2.5

spring-projects-issues commented 18 years ago

Juergen Hoeller commented

Thanks for the suggestion! I've adapted VelocityView to translate a Velocity MethodInvocationException into a Spring NestedServletException, properly keeping the original exception thrown by the invoked method (through Spring's checked exception strategy, overriding the JDK 1.4 getCause method - compatible with both JDK 1.3 and 1.4+).

Juergen