Issue Details (XML | Word | Printable)

Key: GROOVY-2226
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Paul King
Reporter: Yegor Bryukhov
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

Errors in templates syntax produce not so helpful error messages

Created: 19/Oct/07 10:09 AM   Updated: 08/Feb/08 01:26 AM
Component/s: None
Affects Version/s: 1.1-beta-1
Fix Version/s: 1.5.2

Time Tracking:
Not Specified


 Description  « Hide
This code has a problem at least in the last <% %>:
class A {
   def static engine = new groovy.text.SimpleTemplateEngine()
   def static templt = engine.createTemplate('''
   <html>
   <body>
      <% if(a == 1) %>
         a == 1
      <% else %>
         a != 1
      <% %>
   </body>
   </html>
   ''')
}

a = new A()
println a.templt.make([a: 1])

it produces:

Caught: java.lang.ExceptionInInitializerError
        at t.class$(t.groovy)
        at t.run(t.groovy:16)
        at t.main(t.groovy)

which gives no clue that the problem is in the template body (I suppose I always have to use if-statement with curly brackets).
It is almost impossible to track this error in a real environment with big template sitting not even in the code but somewhere else.

If I replace the hole <bod...</body> with <body><%></body> the error is the same.
If I change template variable declaration to non-static, the error is a bit better but still not good:

Caught: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed, Script1.groovy: 5: unexpected token: ; @ line 5, column 33.
1 error

        at A.<init>(t.groovy:3)
        at t.run(t.groovy:16)
        at t.main(t.groovy)

Where this Script1.groovy comes from? I have no ';' in my code ...



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Yegor Bryukhov added a comment - 19/Oct/07 10:13 AM
And when such code is used in Netbeans application, you can even see class not found exceptions which makes you think in a completely different direction.

Yegor Bryukhov added a comment - 19/Oct/07 10:15 AM
And I don't know why JIRA put 11 lines of code into one

Yegor Bryukhov added a comment - 19/Oct/07 10:19 AM
Here is another try to put the correctly formatted code, anyway line 16 in the exception is "a = new A()" and line 3 is [def templt = engine.createTemplate(''']:
--------
class A {
def static engine = new groovy.text.SimpleTemplateEngine()
def static templt = engine.createTemplate('''
<html>
<body>
<% if(a == 1) %>
a == 1
<% else %>
a != 1
<% %>
</body>
</html>
''')
}

a = new A()
println a.templt.make([a: 1])


Paul King added a comment - 20/Oct/07 05:25 AM
Add confluence code tags to description

Paul King added a comment - 20/Oct/07 05:56 AM - edited
As a workaround, I would remove the static modifiers for engine and templt as you are calling them from an instance anyway and also turn on the verbose flag in SimpleTemplateEngine like this:
class A {
   def engine = new groovy.text.SimpleTemplateEngine(true)
   def templt = engine.createTemplate('''
   <html>
   <body>
      <% if(a == 1) %>
         a == 1
      <% else %>
         a != 1
      <% %>
   </body>
   </html>
   ''')
}

a = new A()
println a.templt.make([a: 1])

This then shows you where the problem is:

-- script source --
/* Generated by SimpleTemplateEngine */
out.print("\n");
out.print("   <html>\n");
out.print("   <body>\n");
out.print("      "); if(a == 1) ;
out.print("\n");
out.print("         a == 1\n");
out.print("      "); else ;
out.print("\n");
out.print("         a != 1\n");
out.print("      "); ;
out.print("\n");
out.print("   </body>\n");
out.print("   </html>\n");
out.print("   ");

-- script end --

Caught: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script1.groovy: 5: unexpected token: ; @ line 5, column 33.
1 error

	at A.<init>(testBits.groovy:1327)
	at testBits.run(testBits.groovy:1340)
	at testBits.main(testBits.groovy)

Which gives an indication that you need the curly braces as you have already surmised:

<% if(a == 1) { %>
         a == 1
      <% } else { %>
         a != 1
      <% } %>

Paul King added a comment - 06/Jan/08 03:52 AM
SimpleTemplateEngine, XmlTemplateEngine and GStringTemplateEngine now all produce somewhat more meaningful error messages when producing an incorrect template script. If you see the static initializer problem you can re-run the script using the '-d' argument on the command-line and you will see:
C:\temp>groovy -d Groovy2226.groovy
Caught: java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
[...]
Caused by: groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed, SimpleTemplateScript1.groovy: 5: unexpected token:
; @ line 5, column 33.
1 error
        at groovy.text.SimpleTemplateEngine.createTemplate(SimpleTemplateEngine.java:75)
[...]

Paul King added a comment - 08/Feb/08 01:26 AM
close off release 1.5.4