Skip to content

Instantly share code, notes, and snippets.

@vmassol
Created January 15, 2016 20:30
Show Gist options
  • Save vmassol/c887cb92f76afa5fba4d to your computer and use it in GitHub Desktop.
Save vmassol/c887cb92f76afa5fba4d to your computer and use it in GitHub Desktop.
TryCatchDirective
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.velocity.introspection;
import java.io.IOException;
import java.io.Writer;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
/**
* Allows catching exception from Velocity. Example usage:
* <pre>
* {@code #try()
* ...
* #end
*
* ## At this stage $exception contains the exception if any has been thrown.
* ## It can be tested like this:
* #if ("$!exception" != '')
* ## An exception has been thrown
* #end}
* </pre>
*
* @version $Id: 432f8b6d2499c490e6995f2b7f27067dd58537c9 $
* @since 6.3M1
*/
public class TryCatchDirective extends Directive
{
static final String IN_TRY_DIRECTIVE_KEY_NAME = "insidevelocitytrydirective";
private static final String EXCEPTION_KEY_NAME = "exception";
@Override
public String getName()
{
return "try";
}
@Override
public int getType()
{
return BLOCK;
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
try {
// Make sure to clear any previous exception
context.remove(EXCEPTION_KEY_NAME);
// Set that we're inside a try directive. This is used by the ExceptionCatchingUberspector (for ex).
context.put(IN_TRY_DIRECTIVE_KEY_NAME, true);
return node.jjtGetChild(0).render(context, writer);
} catch (Exception e) {
// Since the Exception could have been caught and rethrown by the Exception Catching Uberspector and since
// in that case the Velocity runtime will wrap it in a VelocityException exception, we need to unwrap it...
Exception originalException = e;
if (originalException instanceof VelocityException) {
Throwable originalThrowable = ((VelocityException) e).getWrappedThrowable();
if (originalThrowable instanceof Exception) {
originalException = (Exception) originalThrowable;
}
}
// Save the exception in the $exception velocity context variable
context.put(EXCEPTION_KEY_NAME, originalException);
return true;
} finally {
// Remove the fact that we're inside a try directive
context.remove(IN_TRY_DIRECTIVE_KEY_NAME);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment