Skip to content

Commit

Permalink
Improve Database connection error messages (joomla#18587)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
brianteeman authored and mbabker committed Nov 18, 2017
1 parent 54eb5d8 commit d14b488
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions libraries/joomla/database/driver/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions libraries/joomla/database/driver/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/database/driver/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions libraries/joomla/database/driver/sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libraries/src/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion libraries/src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/suites/libraries/cms/error/JErrorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit d14b488

Please sign in to comment.