Scalr::errorHandler PHP Method

errorHandler() public static method

Scalr error handler
public static errorHandler ( integer $errno, string $errstr, string $errfile, integer $errline )
$errno integer
$errstr string
$errfile string
$errline integer
    public static function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $level = error_reporting();
        // Handles error suppression.
        if (($level & $errno) === 0) {
            return false;
        }
        //Friendly error name
        switch ($errno) {
            case E_NOTICE:
                $errname = 'E_NOTICE';
                break;
            case E_WARNING:
                $errname = 'E_WARNING';
                break;
            case E_USER_DEPRECATED:
                $errname = 'E_USER_DEPRECATED';
                break;
            case E_STRICT:
                $errname = 'E_STRICT';
                break;
            case E_USER_NOTICE:
                $errname = 'E_USER_NOTICE';
                break;
            case E_USER_WARNING:
                $errname = 'E_USER_WARNING';
                break;
            case E_COMPILE_ERROR:
                $errname = 'E_COMPILE_ERROR';
                break;
            case E_COMPILE_WARNING:
                $errname = 'E_COMPILE_WARNING';
                break;
            case E_CORE_ERROR:
                $errname = 'E_CORE_ERROR';
                break;
            case E_CORE_WARNING:
                $errname = 'E_CORE_WARNING';
                break;
            case E_DEPRECATED:
                $errname = 'E_DEPRECATED';
                break;
            case E_ERROR:
                $errname = 'E_ERROR';
                break;
            case E_PARSE:
                $errname = 'E_PARSE';
                break;
            case E_RECOVERABLE_ERROR:
                $errname = 'E_RECOVERABLE_ERROR';
                break;
            case E_USER_ERROR:
                $errname = 'E_USER_ERROR';
                break;
            default:
                $errname = $errno;
        }
        $message = "Error {$errname} {$errstr}, in {$errfile}:{$errline}\n";
        switch ($errno) {
            case E_CORE_ERROR:
            case E_ERROR:
            case E_USER_ERROR:
            case E_RECOVERABLE_ERROR:
                $exception = new \ErrorException($errname, $errno, 0, $errfile, $errline);
                $message = $message . "Stack trace:\n " . str_replace("\n#", "\n  #", $exception->getTraceAsString());
                @error_log($message);
                throw $exception;
                break;
            case E_USER_NOTICE:
            case E_NOTICE:
                //Ignore for a while.
                break;
            case E_WARNING:
            case E_USER_WARNING:
                $exception = new \ErrorException($errname, $errno, 0, $errfile, $errline);
                $message = $message . "Stack trace:\n  " . str_replace("\n#", "\n  #", $exception->getTraceAsString());
            default:
                @error_log($message);
        }
    }

Usage Example

Example #1
0
 protected function run1($stage)
 {
     $this->console->out(sprintf("Reencrypting %s database from %s/%s to %s/%s!", \Scalr::getContainer()->config->get('scalr.connections.mysql.name'), $this->source->getCryptoAlgo(), $this->source->getCipherMode(), $this->target->getCryptoAlgo(), $this->target->getCipherMode()));
     set_error_handler(function ($code, $message, $file, $line, $context) {
         \Scalr::errorHandler($code, $message, $file, $line, $context);
         if ($code == E_STRICT) {
             throw new Exception($message);
         }
     }, E_USER_ERROR | E_STRICT | E_RECOVERABLE_ERROR | E_ERROR);
     try {
         $this->db->Execute('START TRANSACTION;');
         $this->recrypt('ssh_keys', ['private_key', 'public_key']);
         $this->recrypt('services_ssl_certs', ['ssl_pkey', 'ssl_pkey_password']);
         $this->recrypt('account_user_settings', ['value'], "WHERE `name` = 'security.2fa.ggl.key'", ['user_id', 'name']);
         $this->recrypt('services_chef_servers', ['auth_key', 'v_auth_key']);
         $this->recrypt('variables', ['value'], '', ['name'], $this->globals);
         $this->recrypt('account_variables', ['value'], '', ['name', 'account_id'], $this->globals);
         $this->recrypt('client_environment_variables', ['value'], '', ['name', 'env_id'], $this->globals);
         $this->recrypt('role_variables', ['value'], '', ['name', 'role_id'], $this->globals);
         $this->recrypt('farm_variables', ['value'], '', ['name', 'farm_id'], $this->globals);
         $this->recrypt('farm_role_variables', ['value'], '', ['name', 'farm_role_id'], $this->globals);
         $this->recrypt('server_variables', ['value'], '', ['name', 'server_id'], $this->globals);
         $reflection = new ReflectionClass('Scalr_Environment');
         $method = $reflection->getMethod('getEncryptedVariables');
         $method->setAccessible(true);
         $this->recrypt('client_environment_properties', ['value'], "WHERE `name` IN ('" . implode("','", array_keys($method->invoke(null))) . "')");
         $this->db->Execute("COMMIT;");
     } catch (\Exception $e) {
         $this->rollback($e->getCode(), $e->getMessage());
         restore_error_handler();
         throw $e;
     }
     restore_error_handler();
 }