|
Posted by ImOk on 12/17/89 11:52
This looks like a very good idea. I will have to study it.
But you only need retries for cases where there is a chance of
recovery. E.g. Syntax errors or missing includes will never recover.
Neither will divide by zero.
The main purpose of retries as far as Im concerned has to do with file
and resource connections and locks.
Also using an error handler for unique key checking is better.You dont
have to perform a select first to see if the key already exists. Just
Insert and see if you get an error and retry with a different key.
Tim Hunt wrote:
> ImOk wrote:
> > I just have a question about trapping and retrying errors especially
> > file locking or database locks or duplicate key errors.
> >
> > Is there a way after you trap an error to retry the same line that
> > cause the error?
> >
> > In many other languages you have the option of rertying certain errors.
> > In effect, its like a return to the exact same line. You can then retry
> > certain # of times and then produce an error if it keeps failing.
>
> If you use set_error_handler to collect the errors and debug_backtrace
> to see what caused the error it is possible to retry *some* errors.
>
> I could only get it working if the error occured inside a function. If
> you check the error message given to the event handler then you maybe
> able to handle other errors, not sure if it can be done though.
>
> Anyhoo the code below will try 3 times when a error occurs in any and
> all functions.
>
> <?php
>
> error_reporting(0);
>
> function retryErrorHandler($errNo, $errMsg ) {
>
> static $attempts = 1;
> $maxAttempts = 3;
>
> $backTrace = debug_backtrace();
>
> print "<br><br>Attempt $attempts<br>$errMsg<br>";
>
>
> if ( $attempts >= $maxAttempts ) {
> $attempts = 1;
> } else if ( isset($backTrace[1]) &&
> isset($backTrace[1]['function']) && isset($backTrace[1]['args']) ) {
>
> // $backTrace[0] is info about the call to retryErrorHandler
> // we need info about the previous function that triggered the
> error
> // so $backTrace[1] is used if a function + its arguments are
> available
>
> $attempts+=1;
> $func = $backTrace[1]['function'];
> $args = $backTrace[1]['args'];
>
> print "Retry function $func <br>";
>
> //Hmm..The error handler is only called the second time round if
> the handler is reset
> set_error_handler('retryErrorHandler');
> call_user_func_array( $func, $args );
>
> } else {
> print "Can't retry error: $errMsg<br>";
> $attempts = 1;
> }
>
> }
>
> $old_error_handler = set_error_handler('retryErrorHandler');
>
>
> // trigger a few errors...
>
> // will not retry, not enough info returned by debug_backtrace to try
> again
> $i = UNDEFINED_THING;
>
> // not enough info to try include() errors again - the dodgy filename
> isn't in the backtrace details
> // assume its because include is a language construct not a function
> call
> include('is not a file.php');
>
> // will retry three times
> fopen( 'no file', 'r' );
>
> // will retry three times
> mysql_connect( 'mysql wont connect' );
>
> // will retry user functions as well as internal ones
> testFunc();
>
> function testFunc() {
> $x = 3 / 0;
> }
>
> ?>
Navigation:
[Reply to this message]
|