Pimcore\Db::errorHandler PHP Method

errorHandler() public static method

public static errorHandler ( $method, $args, $exception, boolean $logError = true )
$method
$args
$exception
$logError boolean
    public static function errorHandler($method, $args, $exception, $logError = true)
    {
        if ($logError) {
            Logger::error($exception);
            Logger::error(["message" => $exception->getMessage(), "method" => $method, "arguments" => $args]);
        }
        $lowerErrorMessage = strtolower($exception->getMessage());
        // check if the mysql-connection is the problem (timeout issues, ...)
        if (strpos($lowerErrorMessage, "mysql server has gone away") !== false || strpos($lowerErrorMessage, "lost connection") !== false) {
            // wait a few seconds
            sleep(5);
            // the connection to the server has probably been lost, try to reconnect and call the method again
            try {
                Logger::warning("The connection to the MySQL-Server has probably been lost, try to reconnect...");
                self::reset();
                Logger::warning("Reconnecting to the MySQL-Server was successful, sending the command again to the server.");
                $r = self::get()->callResourceMethod($method, $args);
                Logger::warning("Resending the command was successful");
                return $r;
            } catch (\Exception $e) {
                Logger::error($e);
                throw $e;
            }
        }
        // no handling throw it again
        throw $exception;
    }

Usage Example

Beispiel #1
0
 /**
  * @throws \Exception
  * @param  $method
  * @param  $args
  * @return mixed
  */
 public function __call($method, $args)
 {
     try {
         $r = $this->callResourceMethod($method, $args);
         return $r;
     } catch (\Exception $e) {
         return Db::errorHandler($method, $args, $e);
     }
 }