Forum Moderators: coopster

Message Too Old, No Replies

eval() error handling.

Anyway to capture and handle Parse Errors et al?

         

gethan

11:39 am on Oct 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example typos:

eval("print 3+2");

Results in

Parse error: parse error in /home/www/test.php(2) : eval()'d code on line 1

I would like to capture this and report the error in a nice way - then continue processing other things.

Reading so far I don't think there is a way to do this... I tried output buffering but it is a fatal error and causes the script to halt.

Does anyone either know a better way? Anyway to pre-test the statement? Or can confirm my hunch that it's not possible (yet).

coopster

1:00 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You may have to define your own error handling rules [us2.php.net].

I found this somewhere once, can't remember where I first seen the link though...

h**p://www.mojavelinux.com/cooker/demos/errorReporter/

Maybe you'll get some helpful ideas reviewing the code.

gethan

2:24 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coopster.

There are some really nice error handling features being added. Liked the errorReporter as well... I'll go through it in more detail soon.

Turns out I was being dumb. My initial attempt at output buffering was flawed, but managed to get it working in a much more simple scenario.


//$eval = '$myeval = 3+2; print $myeval;';
$eval = '$myeval = 3+2; print $myeval';

ob_start();
eval($eval);
$ret = ob_get_contents();
ob_end_clean();

print "<P>Not halted :)</P>";

if (preg_match("/Parse error/",$ret)) {
print "<P>A Parse error occured</P>";
// do something smart
} else {
print "<P>The evaled code was fine.</P>";
}
print "<P>Result: $ret</P>";
print "<P>Value: $myeval</P>";

Demonstrates that it works fine.

gethan

9:40 am on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK... so back to this again ;)

The output buffering does not capture fatal errors, so thats where I need to use a custom error handler - which I haven't written yet.

Example: $eval = '$myeval = 3+2; undefined_func($myeval);';

Causes:
Fatal error: Call to undefined function: undefined_func() in /home/www/html/eval.php(7) : eval()'d code on line 1

I will continue to update the thread as I make progress but right now - I have to work on something else ;)