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

Turn Smarty's E_NOTICE into E_ERROR

 
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 -> General
View previous topic :: View next topic  
Author Message
afrancesconi
Smarty Rookie


Joined: 08 Feb 2015
Posts: 6

PostPosted: Sun Mar 01, 2015 2:40 pm    Post subject: Turn Smarty's E_NOTICE into E_ERROR Reply with quote

There is a piece of my code that generates HTML from a Smarty template:

Code:
               try {
                       //....
                       $this->smarty->fetch($this->workingPath."/templates/".$this->getConfig('template'));
               }
               catch (Exception $e) {
                       return "<div class=\"error\">There is a problem! Here is some information: <pre>". $e ."</pre></div>";
               }


As you can see, I would like to catch ANY error coming from the above code, as well as cases when the given template contains errors. The problem is that Smarty doesn't throw a real exception when something bad occurs inside the template, instead it prints a E_NOTICE like this:

Quote:
<br /> <b>Notice</b>: Undefined index: pos in <b>templates\cache\1a97d3e307ccba92c15619e31538268c5dddeb20.file.default.tpl.php</b> on line <b>64</b><br />


Is it possible to turn this notices into exception so I can catch them in that code?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Mar 01, 2015 7:41 pm    Post subject: Reply with quote

This is PHP errors, not Smarty's.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Mar 01, 2015 7:44 pm    Post subject: Reply with quote

Smarty not implemented runtime checks for all possible PHP error like access to an undefined array index.

You can install an error handler which converts PHP errors to exceptions.

Code:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
Smarty::muteExpectedErrors();


You must call Smarty::muteExpectedErrors(); after set_error_handler() to catch some expected errors,
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Mar 02, 2015 1:13 am    Post subject: Reply with quote

If you are going that road, check the http://php.net/manual/en/class.errorexception.php#95415
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Mar 02, 2015 7:43 pm    Post subject: Reply with quote

AnrDaemon wrote:
If you are going that road, check the http://php.net/manual/en/class.errorexception.php#95415


This is not relevant as he wants exceptions on E_NOTICE and other errors caused by the template code.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Mar 02, 2015 8:04 pm    Post subject: Reply with quote

Um, that is actually relevant to understand difference between PHP error_handler and exceptions.
I didn't said he/she should copy it verbatim. That wouldn't work Smile
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 -> General All times are GMT
Page 1 of 1

 
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