On 8/1/07, Nicholas Beckett <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to