Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty 3: simpler error logging, please!
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 6:10 pm    Post subject: Smarty 3: simpler error logging, please! Reply with quote

I was quite happy with the errors that Smarty 2 threw. In 99.9% of occasions it gave me the right hint very quickly.
I'm very dissatisfied with the new error reporting. It's much too long and sometimes doesn't even contain the necessary information.
For instance I just saw this:
Code:

[23-Nov-2010 18:46:31] PHP Fatal error:  Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "./templates/level4_types_join.tpl""  - Unexpected " = ", expected one of: "|" , "*" , "/" , "%" , "+" , "-" , ""&"" , ISIN , ISDIVBY , ISNOTDIVBY , ISEVEN , ISNOTEVEN , ISEVENBY , ISNOTEVENBY , ISODD , ISNOTODD , ISODDBY , ISNOTODDBY , ")" , "==" , "!=" , "(>,gt)" , "(<,lt)" , "(>=,ge)" , "(<=,le)" , "===" , "!==" , "(%,mod)" , "(&&,and)" , "(||,or)" , "xor"' in /var/www/webnn/smarty3/sysplugins/smarty_internal_templatecompilerbase.php:423
Stack trace:
#0 /var/www/webnn/smarty3/sysplugins/smarty_internal_templateparser.php(2802): Smarty_Internal_TemplateCompilerBase->trigger_template_error()
#1 /var/www/webnn/smarty3/sysplugins/smarty_internal_templateparser.php(2867): Smarty_Internal_Templateparser->yy_syntax_error(19, ' = ')
#2 /var/www/webnn/smarty3/sysplugins/smarty_internal_smartytemplatecompiler.php(51): Smarty_Internal_Templateparser->doParse(19, ' = ')
#3 /var/w in /var/www/webnn/smarty3/sysplugins/smarty_internal_templatecompilerbase.php on line 423

Apart from being overly long and thus taking me too long to understand what it means (because I have to read thru the whole stuff to make sure I didn't overlook something) - it doesn't contain the actual line that threw this error. With another error I have even seen things like "in lin" = the term "in line nnn" got truncated!

Could you please consider a "short error mode" option that is more or less like the old smarty 2 style? e.g. for the above all I would like to see would be something like:
Code:

[23-Nov-2010 18:46:31] PHP Fatal error:  Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "./templates/level4_types_join.tpl""  - Unexpected " = " on line 13'

or even shorter:
Code:

[23-Nov-2010 18:46:31] PHP Fatal error:  'SmartyCompilerException': Unexpected " = " on line 13 in "./templates/level4_types_join.tpl""'code]

Really, that new error reporting is driving me crazy. Also, it adds considerable amounts of dead weight to the logs.
Please!
Thanks!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 7:03 pm    Post subject: Reply with quote

The reason this is so verbose is because of the uncaught exception. An uncaught exception throws the complete stack trace along with the error. PHP4 did not support exceptions, so Smarty 2.x did not use them.

With the addition of exceptions to PHP5, Smarty now throws exceptions on critical errors. This makes error handling very flexible, as you have full control of them. Smarty makes no assumption how to handle exceptions, it just throws them for you to catch.

So if you want cleaner errors, catch the exceptions and print them however you like Smile Example:

Code:
try {
   $smarty->fetch('index.tpl');
} catch (Exception $e) {
   echo "Error: " . $e->getMessage();
}


You could even remove the "expected one of ..." text from the error if you prefer less information. The new lexer is very specific about what it wants.
Back to top
View user's profile Send private message Visit poster's website
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 7:17 pm    Post subject: Reply with quote

I do not want to "print" them. Wink
I don't want
- them in the log in this length (but I do want php errors in the log)
- errrors that don't contain the necessary information
(You may not have noticed my remark that information like the line where the error occurred is often missing. Or, sometimes it is in the stack.)
Would it be troublesome to make an option available like I described it? I'm sure I'm not the only one who would prefer the old reporting.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 7:23 pm    Post subject: Reply with quote

Can you give me the source of the line in the template that caused this error? The line number should be right there. I ran a test myself and got something like:

Code:
Error: Syntax Error in template "./templates/index.tpl"  on line 2 "{if $foo la $bar}" too many shorthand attributes


The error itself told me exactly what I did wrong and where. I'm wondering why your example is not showing the line number of the template.

Yes "print" them was an inaccurate term, instead "handle" them however you like. (echo, log, etc.)
Back to top
View user's profile Send private message Visit poster's website
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 8:18 pm    Post subject: Reply with quote

I think the code was this one, note the "= 10". I removed the display code.
Code:

{if ($item.aType == 20)}
            
   {elseif ($item.aType == 30)}
            
   {elseif ($item.aType == 40)}
            
   {elseif ($item.aType >= 15 && $item.aType < 20)}
            
   {elseif ($item.aType = 10)}
            
   {elseif ($item.aType == 90)}
            
{/if}


Well, "handle", "print", whatever. What I meant is that I want a short descriptive message in the php error.log, nothing more. For instance, the message I got for the = typo was overly verbose, even if you remove the stack trace. Most of the errors I get are typos, so I would be quite satisfied with a simple "error on line nn in template.tpl".

Is there an easy way to at least stop outputting the stack trace, maybe by altering some code in Smarty? If not, how would I go about catching that exception and writing the message to the php error log instead printing it to the web? Unfortunately, if I go this route I have to wrap a *lot* of display() calls Sad I would really prefer some option that would reduce the verboseness.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Nov 23, 2010 8:29 pm    Post subject: Reply with quote

It does display source line and line number in my place:

SmartyCompilerException: Syntax Error in template "eval:" on line 9 " {elseif ($item.aType = 10)}" - Unexpected " = ", expected one of: .....

Which version of Smarty3 do you use?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 8:35 pm    Post subject: Reply with quote

I tried your code, here was the result error:

Code:
Error: Syntax Error in template "./templates/index.tpl"  on line 10 "   {elseif ($item.aType = 10)} "  - Unexpected " = ", expected one of: "|" , "*" , "/" , "%" , "+" , "-" , ""&"" , ISIN , ISDIVBY , ISNOTDIVBY , ISEVEN , ISNOTEVEN , ISEVENBY , ISNOTEVENBY , ISODD , ISNOTODD , ISODDBY , ISNOTODDBY , ")" , "==" , "!=" , "(>,gt)" , "(<,lt)" , "(>=,ge)" , "(<=,le)" , "===" , "!==" , "(%,mod)" , "(&&,and)" , "(||,or)" , "xor"


The line number is present, and also the code snippet that shows exactly where the problem is. I'm a bit confused why it was absent in yours.

We could omit the "expected one of ...", but this is sometimes useful. In this case there are quite a few options, but typically it is much fewer. Again, you can snip this out in your own exception handler.

As for handling exceptions, there are many ways to go about them. You don't have to wrap each individual display() call, you could put the try/catch further up in your code, or encapsulate your entire application into it. You could also use set_exception_handler() to assign a PHP function that handles all uncaught exceptions.

To get rid of the stack trace, just use $e->getMessage() like my previous example.
Back to top
View user's profile Send private message Visit poster's website
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 8:47 pm    Post subject: Reply with quote

Here's another one without a line.
Code:

[22-Nov-2010 20:30:30] PHP Fatal error:  Uncaught exception 'SmartyException' with message '{php} is deprecated, set allow_php_tag = true to enable' in <truncated>

I moved my main code yesterday from Smarty 2 to 3 and encountered something weird there. When I changed the smarty path from .../smarty/... to .../smarty3/... it somehow kept looking for plugins and sysplugins in the wrong path. Even after setting SMARTY_DIR explicitely (which I think should not be necessary?). I don't know how I got rid of that, but suddenly it all was fine. Have a look at this:
Quote:

[22-Nov-2010 21:49:11] PHP Notice: function call '_get_plugin_filepath' is unknown or deprecated. in /var/www/webnn/smarty3/sysplugins/smarty_internal_wrapper.php on line 57
[22-Nov-2010 21:49:11] PHP Fatal error: Uncaught exception 'SmartyException' with message 'unknown method '_get_plugin_filepath'' in /var/www/webnn/smarty3/sysplugins/smarty_internal_wrapper.php:117
Stack trace:
#0 /var/www/webnn/smarty/sysplugins/smarty_internal_template.php(973): Smarty_Internal_Wrapper->convert('_get_plugin_fil...', Array)
#1 /var/www/webnn/smarty/plugins/function.html_options.php(32): Smarty_Internal_Template->__call('_get_plugin_fil...', Array)
#2 /var/www/webnn/smarty/plugins/function.html_options.php(32): Smarty_Internal_Template->_get_plugin_filepath('shared', 'escape_special_...')
#3 /var/www/webnn/www/beta3.webnn.info/templates_c/2a2f3432e27515bb01dac4f152b818b7d65ffc59.file.status.tpl.php(55): smarty_function_html_options(Array, Object(Smarty_Internal_Template))
#4 /var/www/webnn/smarty/sysplugins/smarty_internal_template.php(423): include('/var/www/webnn...')
#5 /var/www/webnn/smarty/sysplugins/smarty_internal_template.php(555): Smarty_Internal_Template->renderTemplate()
#6 /var/www/webnn/www in /var/www/webnn/smarty3/sysplugins/smarty_internal_wrapper.php on line 117

Note, how it looks in smarty, although it should look in smarty3.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 8:50 pm    Post subject: Reply with quote

this might help you specifically:

Code:
// top of your application
try {
   // your application starts here
   include(...);
} catch (SmartyCompilerException $e) {
   // handle compiler errors
   echo "Error: " . preg_replace('!expected one of:.*!','',$e->getMessage());
} catch (SmartyException $e) {
   // general Smarty errors
   echo "Error: " . $e->getMessage();
} catch (Exception $e) {
   // general application errors
   echo "Error: " . $e->getMessage();
}


Last edited by mohrt on Tue Nov 23, 2010 8:55 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 8:51 pm    Post subject: Reply with quote

bolero wrote:

I moved my main code yesterday from Smarty 2 to 3 and encountered something weird there. <snip>


You need to clear your compile/cache files. You may also have deprecated function calls, see the BC notes file that ships with Smarty 3.
Back to top
View user's profile Send private message Visit poster's website
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 10:02 pm    Post subject: Reply with quote

Thanks, I'll check if I can wrap my application with that. Many thanks for the tip!

As for the 2->3 problem. I removed the compiled templates each time before I ran the test again. Nevertheless, it switched to the wrong path each time. Could APC caching played a role here?
I was, of course, relying on the error logging to find the last incompatible Smarty 2 functionality. It then somehow defeats that purpose if I have to correct some functionality first before I can use the error logging to find more problems. Wink
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 10:10 pm    Post subject: Reply with quote

I would try restarting apache to see if APC is playing a role. It could be depending on the setup.
Back to top
View user's profile Send private message Visit poster's website
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Tue Nov 23, 2010 10:40 pm    Post subject: Reply with quote

mohrt wrote:
I tried your code, here was the result error:
Code:
on line 10 "   {elseif ($item.aType = 10)}


Hm, I just removed the "=" and get the same problem again. On closer inspection I see now that the error is in the log, but I don't see it when tailing the log (I use a shell alias to tail the php error log for the last lines and show them on the console). The reason for that apparently is that the line of code in the log contains tabs and a CR. It looks like it gets cut-out in an inconvenient way. e.g. that's the original code
Code:

\t\t\t{elseif ($item.aType = 10)}\r\n

and that's how it appears in the log
Code:

[23-Nov-2010 23:04:39] PHP Fatal error:  Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "./templates/level4_types_join.tpl"  on line 13 "\t\t\t{elseif ($item.aType = 10)}\r
"  - Unexpected " = " <snip>\r\n

and in the ssh client it appears like that:
Code:

[23-Nov-2010 23:04:39] PHP Fatal error:  Uncaught exception 'SmartyCompilerExcep
tion' with message 'Syntax Error in template "./templates/level4_types_join.tpl"
"  - Unexpected " = ", expected one of: "|" , "*" , "/" , "%" , "+" , "-" , ""&"
" , ISIN <snip>

So, the whole line containing tabs and the CR is missing (but there is no linewrapping problem as you can see with the next lines). Maybe a problem in tail. I think it would be helpful, anyway, if you would sanitize this message by removing all special characters like \t\r\n from the quoted code snippet.
mohrt wrote:

We could omit the "expected one of ...", but this is sometimes useful.

Yeah, "very sometimes" Wink Maybe an option at least for that verbosity?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Nov 23, 2010 11:54 pm    Post subject: Reply with quote

The SVN has been updated.
We now remove unneccessary whitepace and list expected tokens only if there are less than 5.
Back to top
View user's profile Send private message
bolero
Smarty Rookie


Joined: 22 Nov 2010
Posts: 30

PostPosted: Wed Nov 24, 2010 4:21 pm    Post subject: Reply with quote

I encountered another error which doesn't reveal me a clue of where it is happening. This time I looked at the raw log and there is ineed no line.

Code:

[24-Nov-2010 17:09:51] PHP Warning:  filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for .tpl in /var/www/webnn/smarty3/sysplugins/smarty_internal_resource_file.php on line 70
[24-Nov-2010 17:09:51] PHP Fatal error:  Uncaught exception 'SmartyException' with message 'Unable to read template file '.tpl'' in /var/www/webnn/smarty3/sysplugins/smarty_internal_template.php:144

plus a stack trace of 6 items that doesn't reveal anything useful to me.
There's obviously a variable missing, so the template name is not correctly filled. However, no information where this would be.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP