Re: How to stop (w/o exception)

"Jonathan Revusky" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Well, as for it logging all that stuff, I guess I just sort of assumed
that one would mostly use #stop as a debugging tool, so one would want
all the info in the logs you could get. I suppose it never occurred to
me that you would use the stop instruction as a "normal" part of a
page.

Basically to "fix" this, you would need to patch the source. If you
look at Environment.handleTemplateException, the bit towards the
bottom which is:

if (te instanceof StopException) {
    throw te;
}

if you moved that up to the top of the method, before it does the
logging, then you would have what you want, I guess.

JR

On Tue, Feb 26, 2008 at 8:21 PM, Newman, John W <[email protected]> wrote:
> Well the e.getMessage() does actually return "graceful", and it does skip over the throw clause and swallow the exception.
>
>
>
>
>         protected void process(HttpServletRequest request,
>                         HttpServletResponse response, Template template, TemplateModel model)
>                         throws TemplateException, IOException, ServletException {
>                 try {
>                         template.process(model, response.getWriter());
>                 } catch (StopException e) {
>                         LOG.info("Got StopException: [" + e.getMessage() + "]");
>
>                         if (!"graceful".equals(e.getMessage())) {
>                                 LOG.info("Not graceful, rethrowing");
>                                 throw e;
>                         }
>                         LOG.info("Stopping gracefully.");
>
>                 } finally {
>                         postTemplateProcess(request, response, template, model);
>                 }
>         }
>
>
>  Regardless of what I put in the stop directive, I don't get the exception on the page anymore.  When I use the regular servlet, I do get the exception on the page, which is desired.  But since I can't subclass it correctly, the exception doesn't get handled as it is up in the super class.
>
>  If the reason is not graceful, I get this in my log twice:
>
>  14:12:51,107 ERROR runtime:96 -
>  Encountered stop instruction
>
>  Cause given: gracefdul
>  gracefdul
>
> The problematic instruction:
>  ----------
>  ==> stop [on line 4, column 9 in WEB-INF/inquiry/view.ftl] [on line 4, column 9 in WEB-INF/inquiry/view.ftl]
>   in include "view.ftl" [on line 11, column 9 in WEB-INF/inquiry/index.ftl]
>  ----------
>
>  Java backtrace for programmers:
>  ----------
>  freemarker.core.StopException: gracefdul
>
>         at freemarker.core.StopInstruction.accept(StopInstruction.java:73)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.Environment.include(Environment.java:1378)
>         at freemarker.core.Include.accept(Include.java:155)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.Environment.process(Environment.java:176)
>         at freemarker.template.Template.process(Template.java:232)
>         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.process(FreemarkerServlet.java:168)
>         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.preTemplateProcess(FreemarkerServlet.java:152)
>         at freemarker.ext.servlet.FreemarkerServlet.process(FreemarkerServlet.java:424)
>         at freemarker.ext.servlet.FreemarkerServlet.doGet(FreemarkerServlet.java:366)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>  14:12:51,107  INFO FreemarkerServlet:170 - Got StopException: [gracefdul]
>  14:12:51,107  INFO FreemarkerServlet:172 - Not graceful, rethrowing
>  14:12:51,123 ERROR FreemarkerServlet:183 - gracefdul
>
>
>  If it is graceful, I still get this in my log:
>
>
>
>  Cause given: graceful
>  graceful
>  The problematic instruction:
>  ----------
>  ==> stop [on line 4, column 9 in WEB-INF/inquiry/view.ftl] [on line 4, column 9 in WEB-INF/inquiry/view.ftl]
>   in include "view.ftl" [on line 11, column 9 in WEB-INF/inquiry/index.ftl]
>  ----------
>
>  Java backtrace for programmers:
>  ----------
>  freemarker.core.StopException: graceful
>         at freemarker.core.StopInstruction.accept(StopInstruction.java:73)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.Environment.include(Environment.java:1378)
>         at freemarker.core.Include.accept(Include.java:155)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>         at freemarker.core.Environment.visit(Environment.java:196)
>         at freemarker.core.Environment.process(Environment.java:176)
>         at freemarker.template.Template.process(Template.java:232)
>         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.process(FreemarkerServlet.java:168)
>         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.preTemplateProcess(FreemarkerServlet.java:152)
>         at freemarker.ext.servlet.FreemarkerServlet.process(FreemarkerServlet.java:424)
>         at freemarker.ext.servlet.FreemarkerServlet.doGet(FreemarkerServlet.java:366)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>  14:14:48,110  INFO FreemarkerServlet:170 - Got StopException: [graceful]
>  14:14:48,126  INFO FreemarkerServlet:175 - Stopping gracefully.
>
>
>  So either way I don't want that showing up in the log, I'm just going to revert back and deal with the entire page inside an [#if]... No big deal.
>
>  Possibly for the next release I'd consider an [#stopGracefully /] directive to handle this correctly.  Back in my day of cold fusion there was <cfabort /> which we used from time to time.  Also a quick refactor of the servlet to support more fine grained subclassing would be helpful.
>
>  Thanks!
>
>
>  -----Original Message-----
>  From: [email protected] [mailto:[email protected]] On Behalf Of Jonathan Revusky
>
>
> Sent: Tuesday, February 26, 2008 1:53 PM
>  To: FreeMarker-user
>  Subject: Re: [FreeMarker-user] How to stop (w/o exception)
>
>  Oh, it appears that the stop.getMessage() is not returning the string
>  "graceful", so it's rethrowing. The constructor is putting a bunch of
>  extra info in the message string as well as the cause you added.
>
>  So that would probably work if it was:
>
>  if (stop.getMessage().find("graceful") == -1) throw stop;
>
>  Then as long as the string "graceful" appears in the message, it
>  simply ends gracefully, otherwise it rethrows.
>
>  JR
>
>  On Tue, Feb 26, 2008 at 7:39 PM, Newman, John W <[email protected]> wrote:
>  > catch (StopException stop) {
>  >     if !("graceful".equals(stop.getMessage()) throw stop;
>  >  }
>  >
>  >  That's where I am going, but FreemarkerServlet doesn't really give me a clear point to hook in.  This class could probably use a few simple refactorings like extract method and such at some point.  Some of the methods look like they are doing 6 different things.
>  >
>  >  Pre template process can do it, but the exception handling doesn't really work out...
>  >
>  >         @Override
>  >         public boolean preTemplateProcess(HttpServletRequest request,
>  >                         HttpServletResponse response, Template template, TemplateModel model) {
>  >                 addMyStuffToModel();
>  >                 try {
>  >                         process(request, response, template, model);
>  >                 } catch (TemplateException e) {
>  >                         handleException(e);
>  >                 } catch (IOException e) {
>  >                         handleException(e);
>  >                 } catch (ServletException e) {
>  >                         handleException(e);
>  >                 }
>  >                 return false;
>  >         }
>  >
>  >         // servlet should have this as a protected method,
>  >         // I could then just call super.process(..) in a try catch block.
>  >         protected void process(HttpServletRequest request,
>  >                         HttpServletResponse response, Template template, TemplateModel model)
>  >                         throws TemplateException, IOException, ServletException {
>  >                 try {
>  >                         template.process(model, response.getWriter());
>  >                 } catch (StopException e) {
>  >                         if (!"graceful".equals(e.getMessage())) {
>  >                                 throw e;
>  >                         }
>  >                 } finally {
>  >                         postTemplateProcess(request, response, template, model);
>  >                 }
>  >         }
>  >
>  >
>  >         protected void handleException(Exception e) {
>  >                 LOG.error(e.getMessage(), e);
>  >                 // uh...
>  >         }
>  >
>  >
>  >
>  >  Also it looks like that won't work anyway, the exception doesn't come back and I can't catch it...  Am I doing something wrong?
>  >
>  >
>  >  13:32:22,530 ERROR runtime:96 -
>  >  Encountered stop instruction
>  >
>  >  Cause given: graceful
>  >  graceful
>  >  The problematic instruction:
>  >  ----------
>  >  ==> stop [on line 4, column 9 in WEB-INF/inquiry/view.ftl] [on line 4, column 9 in WEB-INF/inquiry/view.ftl]
>  >   in include "view.ftl" [on line 11, column 9 in WEB-INF/inquiry/index.ftl]
>  >  ----------
>  >
>  >  Java backtrace for programmers:
>  >  ----------
>  >  freemarker.core.StopException: graceful
>  >         at freemarker.core.StopInstruction.accept(StopInstruction.java:73)
>  >         at freemarker.core.Environment.visit(Environment.java:196)
>  >         at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
>  >         at freemarker.core.Environment.visit(Environment.java:196)
>  >         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>  >         at freemarker.core.Environment.visit(Environment.java:196)
>  >         at freemarker.core.Environment.include(Environment.java:1378)
>  >         at freemarker.core.Include.accept(Include.java:155)
>  >         at freemarker.core.Environment.visit(Environment.java:196)
>  >         at freemarker.core.MixedContent.accept(MixedContent.java:92)
>  >         at freemarker.core.Environment.visit(Environment.java:196)
>  >         at freemarker.core.Environment.process(Environment.java:176)
>  >         at freemarker.template.Template.process(Template.java:232)
>  >         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.process(FreemarkerServlet.java:168)
>  >         at edu.upmc.ccweb.core.support.freemarker.FreemarkerServlet.preTemplateProcess(FreemarkerServlet.java:152)
>  >         at freemarker.ext.servlet.FreemarkerServlet.process(FreemarkerServlet.java:424)
>  >         at freemarker.ext.servlet.FreemarkerServlet.doGet(FreemarkerServlet.java:366)
>  >         at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>  >
>  >
>  >
>  >
>  >
>  >  -----Original Message-----
>  >  From: [email protected] [mailto:[email protected]] On Behalf Of Jonathan Revusky
>  >  Sent: Tuesday, February 26, 2008 1:03 PM
>  >  To: FreeMarker-user
>  >  Subject: Re: [FreeMarker-user] How to stop (w/o exception)
>  >
>  >  On Tue, Feb 26, 2008 at 6:48 PM, Newman, John W <[email protected]> wrote:
>  >  > Instead of the macro I'd just put it in an if statement.  Maybe I can try/catch stop exception in my servlet, but I may not always want to catch that.  There should be a [#abort /] directive or something to do that.
>  >
>  >  Well, one possibility is that you use [#stop "graceful"] when you
>  >  intend for the exception to be ignored. And then in the java code, you
>  >  could:
>  >
>  >  try {
>  >    ....
>  >    template.process(dataModel,out);
>  >    ...
>  >  } catch (StopException stop) {
>  >     if !("graceful".equals(stop.getMessage()) throw stop;
>  >  }
>  >
>  >  so you rethrow the exception unless the detail string associated with
>  >  it is "graceful".
>  >
>  >  In any case, Attila's suggestion of using a macro in conjunction with
>  >  #return also works internally by throwing an exception except that it
>  >  gets caught , so it unwinds to the point where the macro was called.
>  >  You know, again, AFAICS, the only way to jump out of an arbitrarilly
>  >  nested structure is by throwing an exception.
>  >
>  >
>  >  >
>  >  >
>  >  >
>  >  >
>  >  >  -----Original Message-----
>  >  >  From: [email protected] [mailto:[email protected]] On Behalf Of Attila Szegedi
>  >  >  Sent: Tuesday, February 26, 2008 12:44 PM
>  >  >  To: FreeMarker-user
>  >  >  Subject: Re: [FreeMarker-user] How to stop (w/o exception)
>  >  >
>  >  >  You could enclose your template body in a:
>  >  >
>  >  >  [#macro actuallyRenderThePage]
>  >  >  ...
>  >  >  [/#macro]
>  >  >  [@actuallyRenderThePage/]
>  >  >
>  >  >  and then use [#return] from within the macro body. Of course, that
>  >  >  doesn't address more complex situations, as stopping gracefully from
>  >  >  several levels of macros or includes. In these cases [#stop/] is the
>  >  >  only reliable mechanism -- you'll just have to catch and ignore it at
>  >  >  your point of Template.process() invocation...
>  >  >
>  >  >  Attila.
>  >  >
>  >  >  On 2008.02.26., at 17:44, Newman, John W wrote:
>  >  >
>  >  >  > Hi,
>  >  >  >
>  >  >  > I tried searching but sourceforge is down... I'm sure this has been
>  >  >  > discussed before.
>  >  >  >
>  >  >  >
>  >  >  > We have the [#stop /] directive which throws an exception.  Is there
>  >  >  > a way to stop processing gracefully?
>  >  >  >
>  >  >  >
>  >  >  > [#if somethingBad]
>  >  >  >    [#stopGracefully /]
>  >  >  > [/#if]
>  >  >  >
>  >  >  > Sure I could do
>  >  >  >
>  >  >  > [#if ! somethingBad]
>  >  >  >    Everything here
>  >  >  > [/#if]
>  >  >  >
>  >  >  > But I'm pretty sure there is a break or stop thing that should work
>  >  >  > here.  Just wondering, thanks.
>  >  >
>  >  >
>  >  >
>  >  >
>  >  >
>  >  >
>  >  >  -------------------------------------------------------------------------
>  >  >  This SF.net email is sponsored by: Microsoft
>  >  >  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  >  >  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  >  >  _______________________________________________
>  >  >  FreeMarker-user mailing list
>  >  >  [email protected]
>  >  >  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>  >  >
>  >  >  -------------------------------------------------------------------------
>  >  >  This SF.net email is sponsored by: Microsoft
>  >  >  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  >  >  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  >  >  _______________________________________________
>  >  >  FreeMarker-user mailing list
>  >  >  [email protected]
>  >  >  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>  >  >
>  >
>  >  -------------------------------------------------------------------------
>  >  This SF.net email is sponsored by: Microsoft
>  >  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  >  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  >  _______________________________________________
>  >  FreeMarker-user mailing list
>  >  [email protected]
>  >  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>  >
>  >  -------------------------------------------------------------------------
>  >  This SF.net email is sponsored by: Microsoft
>  >  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  >  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  >  _______________________________________________
>  >  FreeMarker-user mailing list
>  >  [email protected]
>  >  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>  >
>
>  -------------------------------------------------------------------------
>  This SF.net email is sponsored by: Microsoft
>  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  _______________________________________________
>  FreeMarker-user mailing list
>  [email protected]
>  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
>  -------------------------------------------------------------------------
>  This SF.net email is sponsored by: Microsoft
>  Defy all challenges. Microsoft(R) Visual Studio 2008.
>  http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>  _______________________________________________
>  FreeMarker-user mailing list
>  [email protected]
>  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
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.