Exception handling in Spring-MVC/FreeMarker web applications

12 messages Options
Embed this post
Permalink
Peter_Ford

Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink

While developing a web application using Spring and FreeMarker I've found
that exceptions during template processing (caused, for example, by keys
referenced in the template but missing from the model map) result in stack
traces being thrown out all the way to the client browser. One of the main
reasons for migrating away from JSPs (apart from them being inefficient,
dangerous, difficult to work with, nightmarish to debug, etc., etc., etc.)
is that JSPs have a nasty tendency to send stack traces to the browser. I
was really hoping FreeMarker wouldn't do the same thing. The problem
appears to be that FM sends the stack trace to the same output stream that
the processed output would normally be sent to, and in a web environment
that's going to be the response output stream.

I need to make sure that one way or another template processing exceptions
are caught in a way that lets the application (or the View) throw out a
response.sendError() call (ideally with a configurable error code, but a
hard-coded '500' error would be acceptable) and log the error trace in the
server log. At the very least I need a way to make certain that the client
doesn't see a stack trace, ever, under any circumstances.

So the question is, does FreeMarker (or the Spring FreeMarkerConfigurer or
FreeMarkerView) have any kind of configuration setting to cover for this? I
could probably create a proprietary View class that catches the exception,
but I'd rather stick with the out-of-the-packet code if I can.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Newman, John W

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
Hi -

Take a look at

                configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

RETHROW_HANDLER may not be what you want ... I forget what each does, there are a few different options.  But that's the setting you're after

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Wednesday, October 21, 2009 2:36 PM
To: [hidden email]
Subject: [FreeMarker-user] Exception handling in Spring-MVC/FreeMarker web applications


While developing a web application using Spring and FreeMarker I've found
that exceptions during template processing (caused, for example, by keys
referenced in the template but missing from the model map) result in stack
traces being thrown out all the way to the client browser. One of the main
reasons for migrating away from JSPs (apart from them being inefficient,
dangerous, difficult to work with, nightmarish to debug, etc., etc., etc.)
is that JSPs have a nasty tendency to send stack traces to the browser. I
was really hoping FreeMarker wouldn't do the same thing. The problem
appears to be that FM sends the stack trace to the same output stream that
the processed output would normally be sent to, and in a web environment
that's going to be the response output stream.

I need to make sure that one way or another template processing exceptions
are caught in a way that lets the application (or the View) throw out a
response.sendError() call (ideally with a configurable error code, but a
hard-coded '500' error would be acceptable) and log the error trace in the
server log. At the very least I need a way to make certain that the client
doesn't see a stack trace, ever, under any circumstances.

So the question is, does FreeMarker (or the Spring FreeMarkerConfigurer or
FreeMarkerView) have any kind of configuration setting to cover for this? I
could probably create a proprietary View class that catches the exception,
but I'd rather stick with the out-of-the-packet code if I can.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
That's great. I think I need either IGNORE_HANDLER or perhaps develop my
own custom handler. Either way, this is the key. Thanks!

"Newman, John W" <[hidden email]> wrote on 10/21/2009 01:13:57
PM:

> Hi -
>
> Take a look at
>
>       configuration.
> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
>
> RETHROW_HANDLER may not be what you want ... I forget what each
> does, there are a few different options.  But that's the setting you're
after
>
> -----Original Message-----
[snip]


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
In reply to this post by Peter_Ford
Wednesday, October 21, 2009, 8:36:05 PM, [hidden email] wrote:

> While developing a web application using Spring and FreeMarker I've found
> that exceptions during template processing (caused, for example, by keys
> referenced in the template but missing from the model map) result in stack
> traces being thrown out all the way to the client browser. One of the main
> reasons for migrating away from JSPs (apart from them being inefficient,
> dangerous, difficult to work with, nightmarish to debug, etc., etc., etc.)
> is that JSPs have a nasty tendency to send stack traces to the browser. I
> was really hoping FreeMarker wouldn't do the same thing. The problem
> appears to be that FM sends the stack trace to the same output stream that
> the processed output would normally be sent to, and in a web environment
> that's going to be the response output stream.

The framework authors has *full* control regarding this behavior... We
don't have (unless, they use the
freemarker.ext.servlet.FreemarkerServlet). OK, there is a default
exception handler, which is rather for development/debugging... Maybe
the default should be for production environment, just to be on the
safe-side. OTOH, then developers will complain that why we can't just
print the trace into the browser. You know, someone always hates you,
no mater how you decide... :)

> I need to make sure that one way or another template processing exceptions
> are caught in a way that lets the application (or the View) throw out a
> response.sendError() call (ideally with a configurable error code, but a
> hard-coded '500' error would be acceptable) and log the error trace in the
> server log. At the very least I need a way to make certain that the client
> doesn't see a stack trace, ever, under any circumstances.

(Controlling HTTP response codes is totally out of the reach for the
FreeMarker "core", since it's not HTTP/Servlet dependent.)

> So the question is, does FreeMarker (or the Spring FreeMarkerConfigurer or
> FreeMarkerView) have any kind of configuration setting to cover for this?

Yes, there is a setting called "template_exception_handler" (you can
set it on the same way as "number_format", "locale", etc.). If its
value contains dot, then it is interpreted as class name (of your
exception handler implementation), and the object will be created with
its parameterless constructor. If the value does not contain dot, then
it must be one of these special values: "rethrow", "debug",
"html_debug", "ignore" (case insensitive).

See also:
http://freemarker.org/docs/pgui_config_errorhandling.html
http://freemarker.org/docs/app_faq.html#misc.faq.niceErrorPage.

> I could probably create a proprietary View class that catches the
> exception, but I'd rather stick with the out-of-the-packet code if I
> can.

I hope you don't have to touch the View class... With "rethrow"
FreeMarker itself will not dump the trace into the HTTP response.

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
In reply to this post by Peter_Ford
Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:

> That's great. I think I need either IGNORE_HANDLER or perhaps develop my
> own custom handler. Either way, this is the key. Thanks!

Nah, certainly you want the "rethrow" handler... Unless you want buggy
templates to return a some half-done response without showing any
error message to the user.

> "Newman, John W" <[hidden email]> wrote on 10/21/2009 01:13:57
> PM:
>
>> Hi -
>>
>> Take a look at
>>
>>       configuration.
>> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
>>
>> RETHROW_HANDLER may not be what you want ... I forget what each
>> does, there are a few different options.  But that's the setting you're
> after
>>
>> -----Original Message-----
> [snip]
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
Maybe... but if the handler rethrows the exception, what catches it in the
SpringMVC environment? How would my app know to force a 500 error response
back to the client? I'm trying to get my brain round that. I think I need
to experiment a bit.

Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:

> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
>
> > That's great. I think I need either IGNORE_HANDLER or perhaps develop
my
> > own custom handler. Either way, this is the key. Thanks!
>
> Nah, certainly you want the "rethrow" handler... Unless you want buggy
> templates to return a some half-done response without showing any
> error message to the user.
>
> > "Newman, John W" <[hidden email]> wrote on 10/21/2009
01:13:57

> > PM:
> >
> >> Hi -
> >>
> >> Take a look at
> >>
> >>       configuration.
> >> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
> >>
> >> RETHROW_HANDLER may not be what you want ... I forget what each
> >> does, there are a few different options.  But that's the setting
you're
> > after
> >>
> >> -----Original Message-----
> > [snip]
> >
> >
> >
>
------------------------------------------------------------------------------

> > Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart
your
> > developing skills, take BlackBerry mobile applications to market and
stay

> > ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> > http://p.sf.net/sfu/devconference
> > _______________________________________________
> > FreeMarker-user mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay

> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Newman, John W

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
Now I remember.. the default was html debug I believe.. this shows the yellow trace in the browser, which is a good default for dev.  I think a blurb about switching this for prod would be a good addition to the doco (an earlier part most people actually read).  If you google for a few terms on that page a bit you'll find lots of people have apps in prod without this configured.  =)

So once I found this exists, I switched to rethrow, and now the error is caught by our exception handler bean upstream.  This guy is configured through the stripes framework, I'd imagine spring MVC offers a similar exception handling facility.

The handler gets the template exception, logs it (among other things), and then sends the user to our nice 500 page.  This page has a piece in it to check if the app is running in debug mode, and if so it will render the trace nicely for the developer.


-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: Wednesday, October 21, 2009 4:26 PM
To: Daniel Dekany; FreeMarker-user
Subject: Re: [FreeMarker-user] Exception handling in Spring-MVC/FreeMarker web applications

Maybe... but if the handler rethrows the exception, what catches it in the
SpringMVC environment? How would my app know to force a 500 error response
back to the client? I'm trying to get my brain round that. I think I need
to experiment a bit.

Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:

> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
>
> > That's great. I think I need either IGNORE_HANDLER or perhaps develop
my
> > own custom handler. Either way, this is the key. Thanks!
>
> Nah, certainly you want the "rethrow" handler... Unless you want buggy
> templates to return a some half-done response without showing any
> error message to the user.
>
> > "Newman, John W" <[hidden email]> wrote on 10/21/2009
01:13:57

> > PM:
> >
> >> Hi -
> >>
> >> Take a look at
> >>
> >>       configuration.
> >> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
> >>
> >> RETHROW_HANDLER may not be what you want ... I forget what each
> >> does, there are a few different options.  But that's the setting
you're
> > after
> >>
> >> -----Original Message-----
> > [snip]
> >
> >
> >
>
------------------------------------------------------------------------------

> > Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart
your
> > developing skills, take BlackBerry mobile applications to market and
stay

> > ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> > http://p.sf.net/sfu/devconference
> > _______________________________________________
> > FreeMarker-user mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay

> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
In reply to this post by Peter_Ford
Wednesday, October 21, 2009, 10:25:53 PM, [hidden email] wrote:

> Maybe... but if the handler rethrows the exception, what catches it in the
> SpringMVC environment?

I don't know... that's in the hands of Spring MVC. If it doesn't
handle it properly, you may try to pull some dirty trick in a custom
TemplateExceptionHandler implementation, although best would be if the
thing is fixed in Spring MVC then.

> How would my app know to force a 500 error response
> back to the client? I'm trying to get my brain round that. I think I need
> to experiment a bit.
>
> Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:
>
>> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
>>
>> > That's great. I think I need either IGNORE_HANDLER or perhaps develop
> my
>> > own custom handler. Either way, this is the key. Thanks!
>>
>> Nah, certainly you want the "rethrow" handler... Unless you want buggy
>> templates to return a some half-done response without showing any
>> error message to the user.
>>
>> > "Newman, John W" <[hidden email]> wrote on 10/21/2009
> 01:13:57
>> > PM:
>> >
>> >> Hi -
>> >>
>> >> Take a look at
>> >>
>> >>       configuration.
>> >> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
>> >>
>> >> RETHROW_HANDLER may not be what you want ... I forget what each
>> >> does, there are a few different options.  But that's the setting
> you're
>> > after
>> >>
>> >> -----Original Message-----
>> > [snip]
>> >
>> >
>> >
>>
>>
>> --
>> Best regards,
>>  Daniel Dekany

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Guo Du

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
In reply to this post by Peter_Ford
On Wed, Oct 21, 2009 at 9:25 PM,  <[hidden email]> wrote:
> Maybe... but if the handler rethrows the exception, what catches it in the
> SpringMVC environment? How would my app know to force a 500 error response
> back to the client? I'm trying to get my brain round that. I think I need
> to experiment a bit.
You don't know there would be exception or not unless the template
finish rendering. By default, freemarker will write the rendering
result to http response outputstream in runtime.

If you want send 500 error if there are exception during the
rendering, you may let freemarker render to a temp outputstream. Once
the render finished without exception, then you write the temp
outputstream to your http response outputstrea. If there are
exceptions during rendering, you may handle with 500 error.

-Guo

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
In reply to this post by Daniel Dekany
It looks like it needs a custom handler. It's actually quite easy to set
the handler in the Spring configuration, but setting it to use the Rethrow
handler results in either a stack trace in the browser or a blank page,
depending upon whether or not you have an "error-page" element in the
web.xml to trap 500 errors (with this element in place you get a blank
page).

Daniel Dekany <[hidden email]> wrote on 10/22/2009 01:01:43 AM:

> Wednesday, October 21, 2009, 10:25:53 PM, [hidden email] wrote:
>
> > Maybe... but if the handler rethrows the exception, what catches it in
the
> > SpringMVC environment?
>
> I don't know... that's in the hands of Spring MVC. If it doesn't
> handle it properly, you may try to pull some dirty trick in a custom
> TemplateExceptionHandler implementation, although best would be if the
> thing is fixed in Spring MVC then.
>
> > How would my app know to force a 500 error response
> > back to the client? I'm trying to get my brain round that. I think I
need
> > to experiment a bit.
> >
> > Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:
> >
> >> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
> >>
> >> > That's great. I think I need either IGNORE_HANDLER or perhaps
develop

> > my
> >> > own custom handler. Either way, this is the key. Thanks!
> >>
> >> Nah, certainly you want the "rethrow" handler... Unless you want buggy
> >> templates to return a some half-done response without showing any
> >> error message to the user.
> >>
> >> > "Newman, John W" <[hidden email]> wrote on 10/21/2009
> > 01:13:57
> >> > PM:
> >> >
> >> >> Hi -
> >> >>
> >> >> Take a look at
> >> >>
> >> >>       configuration.
> >> >>
setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

> >> >>
> >> >> RETHROW_HANDLER may not be what you want ... I forget what each
> >> >> does, there are a few different options.  But that's the setting
> > you're
> >> > after
> >> >>
> >> >> -----Original Message-----
> >> > [snip]
> >> >
> >> >
> >> >
> >>
> >>
> >> --
> >> Best regards,
> >>  Daniel Dekany
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay

> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
Monday, October 26, 2009, 5:10:40 PM, [hidden email] wrote:

> It looks like it needs a custom handler. It's actually quite easy to set
> the handler in the Spring configuration, but setting it to use the Rethrow
> handler results in either a stack trace in the browser or a blank page,
> depending upon whether or not you have an "error-page" element in the
> web.xml to trap 500 errors (with this element in place you get a blank
> page).

Just in case you missed it, FreeMarker itself logs the exception. So,
the blank(ish) page might not that bad. (Of course it should not be
really blank, I should say "Sorry, an error has occurred on the
server" etc.)

> Daniel Dekany <[hidden email]> wrote on 10/22/2009 01:01:43 AM:
>
>> Wednesday, October 21, 2009, 10:25:53 PM, [hidden email] wrote:
>>
>> > Maybe... but if the handler rethrows the exception, what catches it in
> the
>> > SpringMVC environment?
>>
>> I don't know... that's in the hands of Spring MVC. If it doesn't
>> handle it properly, you may try to pull some dirty trick in a custom
>> TemplateExceptionHandler implementation, although best would be if the
>> thing is fixed in Spring MVC then.
>>
>> > How would my app know to force a 500 error response
>> > back to the client? I'm trying to get my brain round that. I think I
> need
>> > to experiment a bit.
>> >
>> > Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:
>> >
>> >> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
>> >>
>> >> > That's great. I think I need either IGNORE_HANDLER or perhaps
> develop
>> > my
>> >> > own custom handler. Either way, this is the key. Thanks!
>> >>
>> >> Nah, certainly you want the "rethrow" handler... Unless you want buggy
>> >> templates to return a some half-done response without showing any
>> >> error message to the user.
>> >>
>> >> > "Newman, John W" <[hidden email]> wrote on 10/21/2009
>> > 01:13:57
>> >> > PM:
>> >> >
>> >> >> Hi -
>> >> >>
>> >> >> Take a look at
>> >> >>
>> >> >>       configuration.
>> >> >>
> setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
>> >> >>
>> >> >> RETHROW_HANDLER may not be what you want ... I forget what each
>> >> >> does, there are a few different options.  But that's the setting
>> > you're
>> >> > after
>> >> >>
>> >> >> -----Original Message-----
>> >> > [snip]
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >> --
>> >> Best regards,
>> >>  Daniel Dekany
>>
>> --
>> Best regards,
>>  Daniel Dekany
>>
>>
>>
> ------------------------------------------------------------------------------
>
>> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
>> is the only developer event you need to attend this year. Jumpstart your
>> developing skills, take BlackBerry mobile applications to market and stay
>
>> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
>> http://p.sf.net/sfu/devconference
>> _______________________________________________
>> FreeMarker-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Exception handling in Spring-MVC/FreeMarker web applications

Reply Threaded More More options
Print post
Permalink
I solved this problem a few days ago, without the use of a custom handler.
Just for completeness, and in case any other FreeMarker freaks need to do
the same thing in a Spring-MVC web application, here's the how:

1 - you need an error-page element in your web.xml:

<error-page>
  <error-code>500</error-code>
  <location>/e500.ftl</location>
</error-page>

(In my case, the *.ftl extension is servlet-mapped to the Spring
dispatcher; e500.ftl is my FreeMarker template error page.)

2 - In the Spring configuration, you need to set the correct handler:

<bean id="freemarkerConfig"

class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="freemarkerSettings">
    <props>
      <prop key="template_exception_handler">rethrow</prop>
...

Setting the handler to "rethrow" gets things working exactly the way I want
- template exceptions show my 500 page without a stack trace, and of course
the log file captures the stack trace so I have it for analysis. For
development I can set this to "html_debug", which displays a stack trace I
can use.

Thanks to Daniel Dekany and John Newman for getting me on the right track
with this.

--Pete

Daniel Dekany <[hidden email]> wrote on 10/26/2009 11:58:49 AM:

> Monday, October 26, 2009, 5:10:40 PM, [hidden email] wrote:
>
> > It looks like it needs a custom handler. It's actually quite easy to
set
> > the handler in the Spring configuration, but setting it to use the
Rethrow

> > handler results in either a stack trace in the browser or a blank page,
> > depending upon whether or not you have an "error-page" element in the
> > web.xml to trap 500 errors (with this element in place you get a blank
> > page).
>
> Just in case you missed it, FreeMarker itself logs the exception. So,
> the blank(ish) page might not that bad. (Of course it should not be
> really blank, I should say "Sorry, an error has occurred on the
> server" etc.)
>
> > Daniel Dekany <[hidden email]> wrote on 10/22/2009 01:01:43 AM:
> >
> >> Wednesday, October 21, 2009, 10:25:53 PM, [hidden email] wrote:
> >>
> >> > Maybe... but if the handler rethrows the exception, what catches it
in

> > the
> >> > SpringMVC environment?
> >>
> >> I don't know... that's in the hands of Spring MVC. If it doesn't
> >> handle it properly, you may try to pull some dirty trick in a custom
> >> TemplateExceptionHandler implementation, although best would be if the
> >> thing is fixed in Spring MVC then.
> >>
> >> > How would my app know to force a 500 error response
> >> > back to the client? I'm trying to get my brain round that. I think I
> > need
> >> > to experiment a bit.
> >> >
> >> > Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:08:39 PM:
> >> >
> >> >> Wednesday, October 21, 2009, 9:58:17 PM, [hidden email] wrote:
> >> >>
> >> >> > That's great. I think I need either IGNORE_HANDLER or perhaps
> > develop
> >> > my
> >> >> > own custom handler. Either way, this is the key. Thanks!
> >> >>
> >> >> Nah, certainly you want the "rethrow" handler... Unless you want
buggy
> >> >> templates to return a some half-done response without showing any
> >> >> error message to the user.
> >> >>
> >> >> > "Newman, John W" <[hidden email]> wrote on
10/21/2009

> >> > 01:13:57
> >> >> > PM:
> >> >> >
> >> >> >> Hi -
> >> >> >>
> >> >> >> Take a look at
> >> >> >>
> >> >> >>       configuration.
> >> >> >>
> > setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
> >> >> >>
> >> >> >> RETHROW_HANDLER may not be what you want ... I forget what each
> >> >> >> does, there are a few different options.  But that's the setting
> >> > you're
> >> >> > after
> >> >> >>
> >> >> >> -----Original Message-----
> >> >> > [snip]
> >> >> >
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >> --
> >> >> Best regards,
> >> >>  Daniel Dekany
> >>
> >> --
> >> Best regards,
> >>  Daniel Dekany
> >>
> >>
> >>
> >
>
------------------------------------------------------------------------------

> >
> >> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> >> is the only developer event you need to attend this year. Jumpstart
your
> >> developing skills, take BlackBerry mobile applications to market and
stay

> >
> >> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> >> http://p.sf.net/sfu/devconference
> >> _______________________________________________
> >> FreeMarker-user mailing list
> >> [hidden email]
> >> https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
> >
> >
>
------------------------------------------------------------------------------

> > Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart
your
> > developing skills, take BlackBerry mobile applications to market and
stay

> > ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> > http://p.sf.net/sfu/devconference
> > _______________________________________________
> > FreeMarker-user mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay

> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user