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

[email protected]
Newsgroups gmane.comp.web.freemarker.user
Message-ID <OF7398C65C.1C8A2EF9-ON87257664.006C4EBC-87257664.006DDCD0@blm.gov>
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 <[email protected]> wrote on 10/26/2009 11:58:49 AM:

> Monday, October 26, 2009, 5:10:40 PM, [email protected] 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 <[email protected]> wrote on 10/22/2009 01:01:43 AM:
> >
> >> Wednesday, October 21, 2009, 10:25:53 PM, [email protected] 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 <[email protected]> wrote on 10/21/2009 02:08:39 PM:
> >> >
> >> >> Wednesday, October 21, 2009, 9:58:17 PM, [email protected] 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" <[email protected]> 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
> >> [email protected]
> >> 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
> > [email protected]
> > 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
> [email protected]
> 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
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.