From d14b4880a9c444e41fa32eeac4747afb768b5804 Mon Sep 17 00:00:00 2001 From: Brian Teeman Date: Sat, 18 Nov 2017 14:26:19 +0000 Subject: [PATCH] Improve Database connection error messages (#18587) * Improve Database connection error messages Before this PR you often get very unhelpful and hard to understand error messages For example Error displaying the error page: Application Instantiation Error: Could not connect to MySQL. After this PR that error message would be Error: Failed to start application: Could not connect to MySQL server. You will see in the changes that before this PR we had identical error messages if mysql was not supported in php, mysql server details were wrong, mysql database could not be connected to. After this PR they each have unique and more informative error messages. NOTE postgres and ms sql already had some of these more informative and accurate messages * period * Tweak pgsql message * Tweak SQL Server message --- libraries/joomla/database/driver/mysql.php | 6 +++--- libraries/joomla/database/driver/mysqli.php | 6 +++--- libraries/joomla/database/driver/postgresql.php | 2 +- libraries/joomla/database/driver/sqlsrv.php | 4 ++-- libraries/src/Exception/ExceptionHandler.php | 2 +- libraries/src/Factory.php | 2 +- tests/unit/suites/libraries/cms/error/JErrorPageTest.php | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/joomla/database/driver/mysql.php b/libraries/joomla/database/driver/mysql.php index 9a385db064c40..af43ba4d44af1 100644 --- a/libraries/joomla/database/driver/mysql.php +++ b/libraries/joomla/database/driver/mysql.php @@ -72,13 +72,13 @@ public function connect() // Make sure the MySQL extension for PHP is installed and enabled. if (!self::isSupported()) { - throw new JDatabaseExceptionUnsupported('Could not connect to MySQL.'); + throw new JDatabaseExceptionUnsupported('Make sure the MySQL extension for PHP is installed and enabled.'); } // Attempt to connect to the server. if (!($this->connection = @ mysql_connect($this->options['host'], $this->options['user'], $this->options['password'], true))) { - throw new JDatabaseExceptionConnecting('Could not connect to MySQL.'); + throw new JDatabaseExceptionConnecting('Could not connect to MySQL server.'); } // Set sql_mode to non_strict mode @@ -366,7 +366,7 @@ public function select($database) if (!mysql_select_db($database, $this->connection)) { - throw new JDatabaseExceptionConnecting('Could not connect to database'); + throw new JDatabaseExceptionConnecting('Could not connect to MySQL database.'); } return true; diff --git a/libraries/joomla/database/driver/mysqli.php b/libraries/joomla/database/driver/mysqli.php index 753ff5d60d993..36b975d3e90c1 100644 --- a/libraries/joomla/database/driver/mysqli.php +++ b/libraries/joomla/database/driver/mysqli.php @@ -160,7 +160,7 @@ public function connect() // Make sure the MySQLi extension for PHP is installed and enabled. if (!self::isSupported()) { - throw new JDatabaseExceptionUnsupported('The MySQL adapter mysqli is not available'); + throw new JDatabaseExceptionUnsupported('The MySQLi extension for PHP is not installed or enabled.'); } $this->connection = @mysqli_connect( @@ -170,7 +170,7 @@ public function connect() // Attempt to connect to the server. if (!$this->connection) { - throw new JDatabaseExceptionConnecting('Could not connect to MySQL.'); + throw new JDatabaseExceptionConnecting('Could not connect to MySQL server.'); } // Set sql_mode to non_strict mode @@ -695,7 +695,7 @@ public function select($database) if (!mysqli_select_db($this->connection, $database)) { - throw new JDatabaseExceptionConnecting('Could not connect to database.'); + throw new JDatabaseExceptionConnecting('Could not connect to MySQL database.'); } return true; diff --git a/libraries/joomla/database/driver/postgresql.php b/libraries/joomla/database/driver/postgresql.php index 63f1136755f1c..f5cfc5bd2c012 100644 --- a/libraries/joomla/database/driver/postgresql.php +++ b/libraries/joomla/database/driver/postgresql.php @@ -108,7 +108,7 @@ public function connect() // Make sure the postgresql extension for PHP is installed and enabled. if (!self::isSupported()) { - throw new JDatabaseExceptionUnsupported('PHP extension pg_connect is not available.'); + throw new JDatabaseExceptionUnsupported('The pgsql extension for PHP is not installed or enabled.'); } // Build the DSN for the connection. diff --git a/libraries/joomla/database/driver/sqlsrv.php b/libraries/joomla/database/driver/sqlsrv.php index 06feae67fc07e..09fd95f41b348 100644 --- a/libraries/joomla/database/driver/sqlsrv.php +++ b/libraries/joomla/database/driver/sqlsrv.php @@ -118,7 +118,7 @@ public function connect() // Make sure the SQLSRV extension for PHP is installed and enabled. if (!self::isSupported()) { - throw new JDatabaseExceptionUnsupported('PHP extension sqlsrv_connect is not available.'); + throw new JDatabaseExceptionUnsupported('The sqlsrv extension for PHP is not installed or enabled..'); } // Attempt to connect to the server. @@ -748,7 +748,7 @@ public function select($database) if (!sqlsrv_query($this->connection, 'USE ' . $database, null, array('scrollable' => SQLSRV_CURSOR_STATIC))) { - throw new JDatabaseExceptionConnecting('Could not connect to database'); + throw new JDatabaseExceptionConnecting('Could not connect to SQL Server database.'); } return true; diff --git a/libraries/src/Exception/ExceptionHandler.php b/libraries/src/Exception/ExceptionHandler.php index 65ec6436b8a6c..95d0cd12734d6 100644 --- a/libraries/src/Exception/ExceptionHandler.php +++ b/libraries/src/Exception/ExceptionHandler.php @@ -146,7 +146,7 @@ public static function render($error) header('HTTP/1.1 500 Internal Server Error'); } - $message = 'Error displaying the error page'; + $message = 'Error'; if ($isException) { diff --git a/libraries/src/Factory.php b/libraries/src/Factory.php index eb9b035789a98..e44c88fe90862 100644 --- a/libraries/src/Factory.php +++ b/libraries/src/Factory.php @@ -134,7 +134,7 @@ public static function getApplication($id = null, array $config = array(), $pref { if (!$id) { - throw new \Exception('Application Instantiation Error', 500); + throw new \Exception('Failed to start application', 500); } self::$application = CMSApplication::getInstance($id); diff --git a/tests/unit/suites/libraries/cms/error/JErrorPageTest.php b/tests/unit/suites/libraries/cms/error/JErrorPageTest.php index 9cbbf76749def..5141cc4b4ac88 100644 --- a/tests/unit/suites/libraries/cms/error/JErrorPageTest.php +++ b/tests/unit/suites/libraries/cms/error/JErrorPageTest.php @@ -136,6 +136,6 @@ public function testEnsureTheRenderMethodCorrectlyHandlesNonExceptionClasses() JErrorPage::render($object); $output = ob_get_clean(); - $this->assertEquals('Error displaying the error page', $output); + $this->assertEquals('Error', $output); } }