Skip to content

Commit 02c68b2

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 02c68b2

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

lang/en_us.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ 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.<br/>Ask the website administrator to verify that the database has been created/initialized.<br/>They should also verify that the <code>config/config.php</code> database configuration is correct.';
9798
$strings['InsufficientPermissionsError'] = 'You do not have permission to access this resource';
9899
$strings['MissingReservationResourceError'] = 'A resource was not selected';
99100
$strings['MissingReservationScheduleError'] = 'A schedule was not selected';

lib/Common/ErrorMessages.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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;
1112

1213
private $_resourceKeys = [];
1314
private static $_instance;
@@ -19,6 +20,7 @@ private function __construct()
1920
$this->SetKey(ErrorMessages::MISSING_SCHEDULE, 'MissingReservationScheduleError');
2021
$this->SetKey(ErrorMessages::RESERVATION_NOT_FOUND, 'ReservationNotFoundError');
2122
$this->SetKey(ErrorMessages::RESERVATION_NOT_AVAILABLE, 'ReservationNotAvailable');
23+
$this->SetKey(ErrorMessages::DATABASE_CONNECTION, 'DatabaseConnectionError');
2224
}
2325

2426
/**

lib/Common/Logging/ExceptionHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ 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+
}
49+
50+
call_user_func($this->callback, $errorMessageId);
4651
}
4752
}
4853

lib/Database/MySQL/MySqlConnection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
class DatabaseConnectionException extends Exception
4+
{
5+
}
6+
37
class MySqlConnection implements IDbConnection
48
{
59
private $_dbUser = '';
@@ -43,14 +47,14 @@ public function Connect()
4347
if (!$this->_db) {
4448
$connectError = mysqli_connect_error();
4549
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);
50+
throw new DatabaseConnectionException("Error connecting to database\nError: " . $connectError);
4751
}
4852

4953
$selected = mysqli_select_db($this->_db, $this->_dbName);
5054

5155
if (!$selected) {
5256
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));
57+
throw new DatabaseConnectionException("Error selecting database\nError: " . mysqli_error($this->_db));
5458
}
5559
mysqli_set_charset($this->_db, 'utf8mb4');
5660

0 commit comments

Comments
 (0)