From d71c3e0812e6cf1aba23605acc803d0964e2fd1f Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 15 May 2020 15:05:17 +0000 Subject: [PATCH] Activate test mode for database --- mod/item.php | 2 +- src/Database/Database.php | 11 +++++++++++ tests/include/ApiTest.php | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mod/item.php b/mod/item.php index 85883914fa..9994b1b31d 100644 --- a/mod/item.php +++ b/mod/item.php @@ -720,7 +720,7 @@ function item_post(App $a) { if ($return_path) { DI::baseUrl()->redirect($return_path); } - throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item wasn\'t stored. Last database error: %d %s', DBA::errorNo(), dba::errorMessage())); + throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item wasn\'t stored.')); } $datarray = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]); diff --git a/src/Database/Database.php b/src/Database/Database.php index ad0c857960..faa83d89e0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -21,8 +21,10 @@ namespace Friendica\Database; +use Exception; use Friendica\Core\Config\Cache; use Friendica\Core\System; +use Friendica\DI; use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Util\DateTimeFormat; use Friendica\Util\Profiler; @@ -63,6 +65,7 @@ class Database private $affected_rows = 0; protected $in_transaction = false; protected $in_retrial = false; + protected $testmode = false; private $relation = []; public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = []) @@ -181,6 +184,10 @@ class Database return $this->connected; } + public function setTestmode(bool $test) + { + $this->testmode = $test; + } /** * Sets the logger for DBA * @@ -630,6 +637,10 @@ class Database $error = $this->error; $errorno = $this->errorno; + if ($this->testmode) { + throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args))); + } + $this->logger->error('DB Error', [ 'code' => $this->errorno, 'error' => $this->error, diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 9970ced248..df2c030877 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -71,6 +71,8 @@ class ApiTest extends DatabaseTest /** @var Database $dba */ $dba = $this->dice->create(Database::class); + $dba->setTestmode(true); + /** @var IConfig $config */ $this->config = $this->dice->create(IConfig::class);