From d4f1efe96a09c057729e8a94b78ebad5d274b1e8 Mon Sep 17 00:00:00 2001 From: Wruczek Date: Wed, 30 Jan 2019 15:59:55 +0100 Subject: [PATCH] Throw DB exceptions instead of silently failing --- src/installer/pages/3.php | 7 +++++++ src/private/php/Utils/DatabaseUtils.php | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/installer/pages/3.php b/src/installer/pages/3.php index 658477a..b399ce0 100644 --- a/src/installer/pages/3.php +++ b/src/installer/pages/3.php @@ -43,6 +43,13 @@ if (!empty($_POST)) { // try to connect only if dbconfig is defined if (isset($dbconfig)) { try { + // Enable DB exceptions instead of silent fails + $dbconfig += [ + "option" => [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + ] + ]; + $db = new Medoo($dbconfig); $sqlfiles = []; diff --git a/src/private/php/Utils/DatabaseUtils.php b/src/private/php/Utils/DatabaseUtils.php index 5073d8e..07ea3c8 100644 --- a/src/private/php/Utils/DatabaseUtils.php +++ b/src/private/php/Utils/DatabaseUtils.php @@ -2,6 +2,7 @@ namespace Wruczek\TSWebsite\Utils; use Medoo\Medoo; +use PDO; use Wruczek\TSWebsite\Config; /** @@ -28,7 +29,16 @@ class DatabaseUtils { public function getDb() { if($this->db === null) { try { - $db = new Medoo($this->configUtils->getDatabaseConfig()); + $config = $this->configUtils->getDatabaseConfig(); + + // Enable DB exceptions instead of silent fails + $config += [ + "option" => [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + ] + ]; + + $db = new Medoo($config); } catch (\Exception $e) { TemplateUtils::i()->renderErrorTemplate("DB error", "Connection to database failed", $e->getMessage()); exit;