Better handling of template exceptions in web applications.
Geoff Shuetrim <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <CAB13hhb6jGBO684QVwVxrj5+VaJXVOeavqqiFUvQODi+ZSDM8A@mail.gmail.com> |
I am using Apache Tomcat 6 with Stripes 1.5.6 and Freemarker 2.3.18. I have
been trying to follow FAQ guidance 24 on tidy handling of template
exceptions.
First up, I have tried to change the Freemarker configuration by subclassing
the FreemarkerServlet and including:
@Override
protected Configuration createConfiguration() {
Configuration cfg = super.createConfiguration();
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
return cfg;
}
I have also included:
private void handleServletException(ServletException se,
HttpServletResponse response) throws IOException {
if (response.isCommitted()) {
Writer writer = response.getWriter();
writer.append("<!-- FREEMARKER ERROR MESSAGE STARTS HERE -->"
+ "<script language=javascript>//\"></script>"
+ "<script language=javascript>//\'></script>"
+ "<script language=javascript>//\"></script>"
+ "<script language=javascript>//\'></script>"
+ "</title></xmp></script></noscript></style></object>"
+ "</head></pre></table>"
+ "</form></table></table></table></a></u></i></b>"
+ "<div align=left "
+ "style='background-color:white; color:black; "
+ "display:block; border-top:double; padding:2pt; "
+ "font-size:medium; font-family:Arial,sans-serif; "
+ "font-style: normal; font-variant: normal; "
+ "font-weight: normal; text-decoration: none; "
+ "text-transform: none'>"
+ "<b style='font-size:medium'>FreeMarker template
error!!!</b>"
+ "<pre><xmp>");
writer.append("</xmp></pre></div></html>");
writer.flush();
} else {
logger.info("Response is NOT committed so sending user to a nice
error page.");
response.reset();
response.sendRedirect((new
CustomActionResolver()).getUrlBinding(ErrorActionBean.class));
}
}
When a template exception occurs, after making those changes, the response
has been committed. The content I specify to be written to the response
does get written but it is at the end of a yellow stack trace, which is what
I have been trying to hide from users.
Even if I can get that to work, my preference would be to use full page
buffering but it is not at all clear to me how I should provide the
Writerinstance for the
Template.process(*...*) method when working in a servlet context. The
writer being used by the HTTPServletResponse is accessible from the the
HTTPServletResponse but there is no setter for the writer in the response.
Thoughts on how to solve the problem either way are appreciated.
Regards
Geoff Shuetrim
------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user