You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Nicholas Beckett <Ni...@dataconnection.com> on 2007/08/01 15:04:22 UTC

Template.merge() doesn't throw ParseErrorException

Hi, 

I'm testing an application that uses the Velocity Template engine. I've
deliberately introduced some parse errors in to my .vm file to test
error handling in Template.merge(). The error is a $variable that isn't
present in the context. 

According to the API this should cause merge() to throw a
ParseErrorException, but it doesn't. I do get a log in velocity.log, but
no exception is thrown. 

I know Velocity bug 467 is tracking a similar issue (Velocity should
throw more Exceptions), but this case seems a little different, in so
much as the merge() method doesn't match the published API. 

Has anyone else seen this? Can anyone suggest an alternative way of
detecting parse errors from within the application? 

Thanks, 

Nicholas


Code extract:

      try
      {
        lTemplate = Velocity.getTemplate(lTemplateName);       
        lTemplate.merge(xiContext, lWriter);      
      }
      catch(ParseErrorException e)
      {
        throw new TemplateException(e.getMessage(), 
                                    "Failed to parse template for " + 
                                    xiTemplateType, 
                                    lTemplateName); 
      }

.vm file extract:
          
#set($Variable=$imnothere.notamethod())\r\n
$Variable things to do\r\n"

velocity.log extract:

41:57,352 - RHS of #set statement is null. Context will not be modified.
snstemplates/fax-urgent-body-en_US.vm [line 5, column 1]
2007-07-27 14:41:57,352 - Null reference [template
'snstemplates/fax-urgent-body-en_US.vm', line 6, column 1] : $Variable
cannot be resolved.

RE: Template.merge() doesn't throw ParseErrorException

Posted by Ryan Smith <ry...@authoria.com>.
According to the documentation:
"If this configuration setting is false or omitted then the page will be
processed as normal, but all invalid references will be collected in a
List of InvalidReferenceInfo objects."

I would love to start using this.  What is the recommended way of
getting to the internal list of invalid references?

Ryan

-----Original Message-----
From: Nathan Bubna [mailto:nbubna@gmail.com] 
Sent: Wednesday, August 01, 2007 10:54 AM
To: Velocity Developers List
Subject: Re: Template.merge() doesn't throw ParseErrorException

On 8/1/07, Nicholas Beckett <Ni...@dataconnection.com> wrote:
> Hi,
>
> I'm testing an application that uses the Velocity Template engine.
I've
> deliberately introduced some parse errors in to my .vm file to test
> error handling in Template.merge(). The error is a $variable that
isn't
> present in the context.
>
> According to the API this should cause merge() to throw a
> ParseErrorException, but it doesn't. I do get a log in velocity.log,
but
> no exception is thrown.

Where did you read that a ParseErrorException would or should be
thrown if a $variable isn't present in the context?  That's never been
standard.  To get a missing reference to throw an exception, you would
need to configure a special event handler to do that.

> I know Velocity bug 467 is tracking a similar issue (Velocity should
> throw more Exceptions), but this case seems a little different, in so
> much as the merge() method doesn't match the published API.
>
> Has anyone else seen this? Can anyone suggest an alternative way of
> detecting parse errors from within the application?

I believe you are looking for something like this:

http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/app/
event/implement/ReportInvalidReferences.html

> Thanks,
>
> Nicholas
>
>
> Code extract:
>
>       try
>       {
>         lTemplate = Velocity.getTemplate(lTemplateName);
>         lTemplate.merge(xiContext, lWriter);
>       }
>       catch(ParseErrorException e)
>       {
>         throw new TemplateException(e.getMessage(),
>                                     "Failed to parse template for " +
>                                     xiTemplateType,
>                                     lTemplateName);
>       }
>
> .vm file extract:
>
> #set($Variable=$imnothere.notamethod())\r\n
> $Variable things to do\r\n"
>
> velocity.log extract:
>
> 41:57,352 - RHS of #set statement is null. Context will not be
modified.
> snstemplates/fax-urgent-body-en_US.vm [line 5, column 1]
> 2007-07-27 14:41:57,352 - Null reference [template
> 'snstemplates/fax-urgent-body-en_US.vm', line 6, column 1] : $Variable
> cannot be resolved.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: Template.merge() doesn't throw ParseErrorException

Posted by Nathan Bubna <nb...@gmail.com>.
On 8/1/07, Nicholas Beckett <Ni...@dataconnection.com> wrote:
> Hi,
>
> I'm testing an application that uses the Velocity Template engine. I've
> deliberately introduced some parse errors in to my .vm file to test
> error handling in Template.merge(). The error is a $variable that isn't
> present in the context.
>
> According to the API this should cause merge() to throw a
> ParseErrorException, but it doesn't. I do get a log in velocity.log, but
> no exception is thrown.

Where did you read that a ParseErrorException would or should be
thrown if a $variable isn't present in the context?  That's never been
standard.  To get a missing reference to throw an exception, you would
need to configure a special event handler to do that.

> I know Velocity bug 467 is tracking a similar issue (Velocity should
> throw more Exceptions), but this case seems a little different, in so
> much as the merge() method doesn't match the published API.
>
> Has anyone else seen this? Can anyone suggest an alternative way of
> detecting parse errors from within the application?

I believe you are looking for something like this:

http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/app/event/implement/ReportInvalidReferences.html

> Thanks,
>
> Nicholas
>
>
> Code extract:
>
>       try
>       {
>         lTemplate = Velocity.getTemplate(lTemplateName);
>         lTemplate.merge(xiContext, lWriter);
>       }
>       catch(ParseErrorException e)
>       {
>         throw new TemplateException(e.getMessage(),
>                                     "Failed to parse template for " +
>                                     xiTemplateType,
>                                     lTemplateName);
>       }
>
> .vm file extract:
>
> #set($Variable=$imnothere.notamethod())\r\n
> $Variable things to do\r\n"
>
> velocity.log extract:
>
> 41:57,352 - RHS of #set statement is null. Context will not be modified.
> snstemplates/fax-urgent-body-en_US.vm [line 5, column 1]
> 2007-07-27 14:41:57,352 - Null reference [template
> 'snstemplates/fax-urgent-body-en_US.vm', line 6, column 1] : $Variable
> cannot be resolved.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org