|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
afrancesconi Smarty Rookie
Joined: 08 Feb 2015 Posts: 6
|
Posted: Sun Mar 01, 2015 2:40 pm Post subject: Turn Smarty's E_NOTICE into E_ERROR |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Mar 01, 2015 7:41 pm Post subject: |
|
|
This is PHP errors, not Smarty's. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Mar 01, 2015 7:44 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Mar 02, 2015 7:43 pm Post subject: |
|
|
This is not relevant as he wants exceptions on E_NOTICE and other errors caused by the template code. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Mar 02, 2015 8:04 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
|
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
|