Re: Better handling of template exceptions in web applications.

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, July 29, 2011, 3:24:47 AM, Geoff Shuetrim wrote:

[snip]

> 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 Writer instance 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.

I suppose it's clear that you can pass whatever Writer to
Template.process as a parameter, and FreeMarker doesn't care where did
you get that and what it does. If you can't make the Servlet API to
give you a full-buffering Writer (which is then not a
FreeMarker-related issue), you can still create your own Writer that
wraps the ServletResponse Writer, buffers everything, and flushes into
the wrapped Writer on flush() and close(), and pass that Writer to
FreeMarker.

One thing you should be aware of is that by default FreeMarker flushed
the writer at the end of Template.process if there was no exception.
This can be a problem if web pages are built from multiple independent
templates that are called with Template.process (rather than with
#include). You can fix this with Configurable.setAutoFlush(false).

Also, FreeMarker templates can call Writer.flush() with <#flush>. I
don't think you need to suppress that, but you can, if your Writer
simply ignores flush() calls.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.