What are different ways to handle error in FreeMarker template?

Noman Arain picture Noman Arain · Feb 27, 2013 · Viewed 8.7k times · Source

How to suppress FreeMarker template error? I am looking here: http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html But I do not understand how to "TemplateExceptionHandler.IGNORE_HANDLER." I am using Struts2 and also how to show another ftl page instead of showing the stack trace?

class MyTemplateExceptionHandler implements TemplateExceptionHandler {
    public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out)
            throws TemplateException {
        try {
            out.write("[ERROR: " + te.getMessage() + "]");
        } catch (IOException e) {
            throw new TemplateException("Failed to print error message. Cause: " + e, env);
        }
    }
}

...

cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());

Found the above piece at http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html How do I use this? That last line, where does cfg come from?

"Main entry point into the FreeMarker API"... http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html

So, that is the main entry point, I am guessing cfg comes from this class. I am still not seeing how the controller will come into my class MyTemplateExceptionHandler.

Where will the following line needs to go?

cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());

And is it just a matter of placing this line in correct spot?

This is how my current class looks like:

    import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Properties;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.cache.WebappTemplateLoader;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.jsp.TaglibFactory;
import freemarker.ext.servlet.HttpRequestHashModel;
import freemarker.ext.servlet.HttpRequestParametersHashModel;
import freemarker.ext.servlet.HttpSessionHashModel;
import freemarker.ext.servlet.ServletContextHashModel;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;

import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.freemarker.FreemarkerManager;
import org.apache.struts2.views.freemarker.ScopesHashModel;
import org.apache.struts2.views.freemarker.StrutsBeanWrapper;
import org.apache.struts2.views.freemarker.StrutsClassTemplateLoader;
import org.omg.CORBA.PUBLIC_MEMBER;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.FileManager;
import com.opensymphony.xwork2.util.ValueStack;

public class MyTemplateExceptionHandler extends org.apache.struts2.views.freemarker.FreemarkerManager {

    freemarker.template.Configuration configuration = new freemarker.template.Configuration();

    public MyTemplateExceptionHandler() {
        System.out.println("MyTemplateExceptionHandler constructor()");
        configuration.setTemplateExceptionHandler(new Test1());
    }

    class Test1 implements TemplateExceptionHandler {

        @Override
        public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out) throws TemplateException {
            System.out.println("MyTemplateExceptionHandler1 handleTemplateException()");
            try {
                out.write("[ERROR TEST TEST: " + te.getMessage() + "]");
            } catch (IOException e) {
                throw new TemplateException("Failed to print error message. Cause: " + e, env);
            }
        }
    }
}

My code is going into MyTemplateExceptionHandler constructor(). But not into MyTemplateExceptionHandler1 handleTemplateException(). What do I need to do?

I am still seeing the yellow FTL stack trace.

Same thing is being pointed out on this blog: http://blog.cherouvim.com/freemarker-exception-handling/ Where excatly do I configure my freemarker and how? I am still stuck as to where that line needs to go.

My other question is, the class posted on the blog seems to be an inner class, do I just put that inner class into any class or is that an outer class?

Answer

Cedric Reichenbach picture Cedric Reichenbach · Feb 28, 2013

If you want to handle it inside freemarker, use its attempt-recover mechanism:

<#attempt>
  attempt block
<#recover>
  recover block
</#attempt>

It's analogous to Java's try-catch.