Skip to content

Commit c9844d4

Browse files
feat: provide a helpful error message if no DB access
If unable to access the database provide a helpful error message. Now the error message will say: Unable to connect to the database. Ask the website administrator to verify that the database has been created/initialized. They should also verify that the config/config.php database configuration is correct. Previously it said: Unknown Error
1 parent c996aa5 commit c9844d4

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

lang/en_us.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ protected function _LoadStrings()
9494
$strings['Error'] = 'Error';
9595
$strings['ReturnToPreviousPage'] = 'Return to the last page that you were on';
9696
$strings['UnknownError'] = 'Unknown Error';
97+
$strings['DatabaseConnectionError'] = 'Unable to connect to the database server.<br/>Ask the website administrator to verify the database host, username, and password in <code>config/config.php</code>.';
98+
$strings['DatabaseNotFoundError'] = 'Unable to select the configured database.<br/>Ask the website administrator to verify the database name in <code>config/config.php</code> and confirm the database has been created/initialized.';
9799
$strings['InsufficientPermissionsError'] = 'You do not have permission to access this resource';
98100
$strings['MissingReservationResourceError'] = 'A resource was not selected';
99101
$strings['MissingReservationScheduleError'] = 'A schedule was not selected';

lib/Common/ErrorMessages.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class ErrorMessages
88
public const MISSING_SCHEDULE = 3;
99
public const RESERVATION_NOT_FOUND = 4;
1010
public const RESERVATION_NOT_AVAILABLE = 5;
11+
public const DATABASE_CONNECTION = 6;
12+
public const DATABASE_NOT_FOUND = 7;
1113

1214
private $_resourceKeys = [];
1315
private static $_instance;
@@ -19,6 +21,8 @@ private function __construct()
1921
$this->SetKey(ErrorMessages::MISSING_SCHEDULE, 'MissingReservationScheduleError');
2022
$this->SetKey(ErrorMessages::RESERVATION_NOT_FOUND, 'ReservationNotFoundError');
2123
$this->SetKey(ErrorMessages::RESERVATION_NOT_AVAILABLE, 'ReservationNotAvailable');
24+
$this->SetKey(ErrorMessages::DATABASE_CONNECTION, 'DatabaseConnectionError');
25+
$this->SetKey(ErrorMessages::DATABASE_NOT_FOUND, 'DatabaseNotFoundError');
2226
}
2327

2428
/**

lib/Common/Logging/ExceptionHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ public function HandleException($exception)
4242
ob_start();
4343
debug_print_backtrace();
4444
error_log(ob_get_clean());
45-
call_user_func($this->callback);
45+
$errorMessageId = ErrorMessages::UNKNOWN_ERROR;
46+
if (is_a($exception, 'DatabaseConnectionException')) {
47+
$errorMessageId = ErrorMessages::DATABASE_CONNECTION;
48+
} elseif (is_a($exception, 'DatabaseNotFoundException')) {
49+
$errorMessageId = ErrorMessages::DATABASE_NOT_FOUND;
50+
}
51+
52+
call_user_func($this->callback, $errorMessageId);
4653
}
4754
}
4855

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
class DatabaseException extends Exception
4+
{
5+
}
6+
7+
class DatabaseConnectionException extends DatabaseException
8+
{
9+
}
10+
11+
class DatabaseNotFoundException extends DatabaseException
12+
{
13+
}

lib/Database/MySQL/MySqlConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function Connect()
4343
if (!$this->_db) {
4444
$connectError = mysqli_connect_error();
4545
Log::Error("Error connecting to database\nCheck your database settings in the config file\n%s", $connectError);
46-
throw new Exception("Error connecting to database\nError: " . $connectError);
46+
throw new DatabaseConnectionException("Error connecting to database\nError: " . $connectError);
4747
}
4848

4949
$selected = mysqli_select_db($this->_db, $this->_dbName);
5050

5151
if (!$selected) {
5252
Log::Error("Error selecting database '%s'\nCheck your database settings in the config file\n%s", $this->_dbName, mysqli_error($this->_db));
53-
throw new Exception("Error selecting database\nError: " . mysqli_error($this->_db));
53+
throw new DatabaseNotFoundException("Error selecting database\nError: " . mysqli_error($this->_db));
5454
}
5555
mysqli_set_charset($this->_db, 'utf8mb4');
5656

lib/Database/namespace.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_once(ROOT_DIR . 'lib/Database/IReader.php');
66
require_once(ROOT_DIR . 'lib/Database/Parameter.php');
77
require_once(ROOT_DIR . 'lib/Database/Parameters.php');
8+
require_once(ROOT_DIR . 'lib/Database/DatabaseExceptions.php');
89
require_once(ROOT_DIR . 'lib/Database/SqlCommand.php');
910
require_once(ROOT_DIR . 'lib/Database/Database.php');
1011
require_once(ROOT_DIR . 'lib/Database/DatabaseFactory.php');

0 commit comments

Comments
 (0)