mirror of
https://github.com/friendica/friendica
synced 2024-11-10 09:02:53 +00:00
Introduce a "DatabaseException" class for fatal exceptions (used in testmode to throw an exception in case of DB errors)
This commit is contained in:
parent
0f3e4255ca
commit
3587e89482
2 changed files with 46 additions and 29 deletions
|
@ -21,10 +21,8 @@
|
||||||
|
|
||||||
namespace Friendica\Database;
|
namespace Friendica\Database;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Friendica\Core\Config\Cache;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
use Friendica\DI;
|
|
||||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||||
use Friendica\Util\DateTimeFormat;
|
use Friendica\Util\DateTimeFormat;
|
||||||
use Friendica\Util\Profiler;
|
use Friendica\Util\Profiler;
|
||||||
|
@ -75,7 +73,6 @@ class Database
|
||||||
$this->profiler = $profiler;
|
$this->profiler = $profiler;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
|
||||||
$this->readServerVariables($server);
|
|
||||||
$this->connect();
|
$this->connect();
|
||||||
|
|
||||||
if ($this->isConnected()) {
|
if ($this->isConnected()) {
|
||||||
|
@ -84,30 +81,6 @@ class Database
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function readServerVariables(array $server)
|
|
||||||
{
|
|
||||||
// Use environment variables for mysql if they are set beforehand
|
|
||||||
if (!empty($server['MYSQL_HOST'])
|
|
||||||
&& (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
|
|
||||||
&& $server['MYSQL_PASSWORD'] !== false
|
|
||||||
&& !empty($server['MYSQL_DATABASE']))
|
|
||||||
{
|
|
||||||
$db_host = $server['MYSQL_HOST'];
|
|
||||||
if (!empty($server['MYSQL_PORT'])) {
|
|
||||||
$db_host .= ':' . $server['MYSQL_PORT'];
|
|
||||||
}
|
|
||||||
$this->configCache->set('database', 'hostname', $db_host);
|
|
||||||
unset($db_host);
|
|
||||||
if (!empty($server['MYSQL_USERNAME'])) {
|
|
||||||
$this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
|
|
||||||
} else {
|
|
||||||
$this->configCache->set('database', 'username', $server['MYSQL_USER']);
|
|
||||||
}
|
|
||||||
$this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
|
|
||||||
$this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function connect()
|
public function connect()
|
||||||
{
|
{
|
||||||
if (!is_null($this->connection) && $this->connected()) {
|
if (!is_null($this->connection) && $this->connected()) {
|
||||||
|
@ -124,6 +97,11 @@ class Database
|
||||||
if (count($serverdata) > 1) {
|
if (count($serverdata) > 1) {
|
||||||
$port = trim($serverdata[1]);
|
$port = trim($serverdata[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty(trim($this->configCache->get('database', 'port')))) {
|
||||||
|
$port = trim(trim($this->configCache->get('database', 'port')));
|
||||||
|
}
|
||||||
|
|
||||||
$server = trim($server);
|
$server = trim($server);
|
||||||
$user = trim($this->configCache->get('database', 'username'));
|
$user = trim($this->configCache->get('database', 'username'));
|
||||||
$pass = trim($this->configCache->get('database', 'password'));
|
$pass = trim($this->configCache->get('database', 'password'));
|
||||||
|
@ -658,7 +636,7 @@ class Database
|
||||||
$errorno = $this->errorno;
|
$errorno = $this->errorno;
|
||||||
|
|
||||||
if ($this->testmode) {
|
if ($this->testmode) {
|
||||||
throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args)));
|
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->error('DB Error', [
|
$this->logger->error('DB Error', [
|
||||||
|
@ -761,7 +739,7 @@ class Database
|
||||||
$errorno = $this->errorno;
|
$errorno = $this->errorno;
|
||||||
|
|
||||||
if ($this->testmode) {
|
if ($this->testmode) {
|
||||||
throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $params)));
|
throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->error('DB Error', [
|
$this->logger->error('DB Error', [
|
||||||
|
|
39
src/Database/DatabaseException.php
Normal file
39
src/Database/DatabaseException.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Friendica\Database;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A database fatal exception, which shouldn't occur
|
||||||
|
*/
|
||||||
|
class DatabaseException extends Exception
|
||||||
|
{
|
||||||
|
protected $query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct the exception. Note: The message is NOT binary safe.
|
||||||
|
*
|
||||||
|
* @link https://php.net/manual/en/exception.construct.php
|
||||||
|
*
|
||||||
|
* @param string $message The Database error message.
|
||||||
|
* @param int $code The Database error code.
|
||||||
|
* @param string $query The Database error query.
|
||||||
|
* @param Throwable $previous [optional] The previous throwable used for the exception chaining.
|
||||||
|
*/
|
||||||
|
public function __construct(string $message, int $code, string $query, Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
$this->query = $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue