Search code examples
phpfpm

Php-fpm still logging my error even when using catch


Php-fpm error log file is still logging my error even using try-catch

$NUM_OF_ATTEMPTS = 100;
$attempts = 0;
        do
            {
            try
                {
                $db = new SQLite3('proxies/socks5.db');
                $results = $db->query('SELECT proxy FROM socks5proxies WHERE timeout <= ' . $settimeout . $countryq . ';');
                while ($row = $results->fetchArray())
                    {
                    echo $row['proxy'] . "\r\n";
                    }
                }

            catch(Exception $e)
                {
                $attempts++;
                sleep(1);
                continue;
                }

            break;
            }

        while ($attempts < $NUM_OF_ATTEMPTS);

Expected result:

Retry on error, and don't log the error

Actual results:

Logs the error in the php-fpm error log file: thrown in /var/www/html/api.php on line 200 [10-Jan-2019 14:00:49 UTC] PHP Warning: SQLite3::query(): Unable to prepare statement: 11, database disk image is malformed in /var/www/html/api.php on line 140 [10-Jan-2019 14:00:49 UTC] PHP Fatal error: Uncaught Error: Call to a member function fetchArray() on boolean in /var/www/html/api.php:141 Stack trace: #0 {main} thrown in /var/www/html/api.php on line 141


Solution

  • Call SQLite3::enableExceptions to tell PHP it should throw exceptions instead of standard errors:

    try {
        $db = new SQLite3('proxies/socks5.db');
        $db->enableExceptions(true);
        $results = $db->query('...');
    } catch (\Exception $e) {
    }
    

    In any case, if you need to do 100 attempts to get this to work, then this really isn't the angle you should be taking to fix it.