Better error reporting

main
Yasen Pramatarov 2024-08-17 11:20:08 +03:00
parent 09667920a2
commit 909fbc2626
11 changed files with 39 additions and 27 deletions

View File

@ -1,17 +1,19 @@
<?php
require '../app/helpers/errors.php';
class Database {
private $pdo;
public function __construct($options) {
// pdo needed
if ( !extension_loaded('pdo') ) {
throw new Exception('PDO extension not loaded.');
$error = getError('PDO extension not loaded.');
}
// options check
if (empty($options['type'])) {
throw new Exception('Database type is not set.');
$error = getError('Database type is not set.');
}
// database type
@ -23,19 +25,19 @@ class Database {
$this->connectMysql($options);
break;
default:
throw newException("Database type \"{$options['type']}\" is not supported.");
$error = getError("Database type \"{$options['type']}\" is not supported.");
}
}
private function connectSqlite($options) {
// pdo_sqlite extension is needed
if (!extension_loaded('pdo_sqlite')) {
throw new Exception('PDO extension for SQLite not loaded.');
$error = getError('PDO extension for SQLite not loaded.');
}
// SQLite options
if (empty($options['dbFile']) || !file_exists($options['dbFile'])) {
throw new Exception('SQLite database file not found.');
$error = getError("SQLite database file \"{$dbFile}\" not found.");
}
// connect to SQLite
@ -43,19 +45,19 @@ class Database {
$this->pdo = new PDO("sqlite:" . $options['dbFile']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('SQLite connection failed: ' . $e->getMessage());
$error = getError('SQLite connection failed: ', $e->getMessage());
}
}
private function connectMysql($options) {
// pdo_mysql extension is needed
if (!extension_loaded('pdo_mysql')) {
throw new Exception('PDO extension for MySQL not loaded.');
$error = getError('PDO extension for MySQL not loaded.');
}
// MySQL options
if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) {
throw new Exception('MySQL connection data is missing.');
$error = getError('MySQL connection data is missing.');
}
// Connect to MySQL
@ -64,7 +66,7 @@ class Database {
$this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? '');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('MySQL connection failed: ' . $e->getMessage());
$error = getError('MySQL connection failed: ', $config['environment'], $e->getMessage());
}
}

View File

@ -29,7 +29,7 @@ return [
// system info
'version' => '0.1.1',
// development has verbose error messages, production has not
'environment' => 'production',
'environment' => 'production1',
// *************************************
// Maintained by the app, edit with care

View File

@ -7,19 +7,15 @@ function connectDB($config, $database = '', $platform_id = '') {
if ($database === 'jilo') {
try {
$dbFile = $config['platforms'][$platform_id]['jilo_database'] ?? null;
if (!$dbFile) {
throw new Exception("Invalid platform ID \"$platform_id\", database file not found.");
if (!$dbFile || !file_exists($dbFile)) {
throw new Exception(getError("Invalid platform ID \"{$platform_id}\", database file \"{$dbFile}\"not found."));
}
$db = new Database([
'type' => 'sqlite',
'dbFile' => $dbFile,
]);
} catch (Exception $e) {
if ($config['environment'] === 'production') {
$error = 'There was an unexpected error. Please try again.';
} else {
$error = 'Error: ' . $e->getMessage();
}
$error = getError('Error connecting to DB.', $e->getMessage());
include '../app/templates/block-message.php';
exit();
}
@ -36,7 +32,7 @@ function connectDB($config, $database = '', $platform_id = '') {
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
$error = getError('Error connecting to DB.', $e->getMessage());
include '../app/templates/block-message.php';
exit();
}
@ -53,7 +49,7 @@ function connectDB($config, $database = '', $platform_id = '') {
]);
$pdo = $db->getConnection();
} catch (Exception $e) {
$error = 'Error: ' . $e->getMessage();
$error = getError('Error connecting to DB.', $e->getMessage());
include '../app/templates/block-message.php';
exit();
}

View File

@ -0,0 +1,14 @@
<?php
function getError($message, $error = '', $environment = null) {
global $config;
$environment = $config['environment'] ?? 'production';
if ($environment === 'production') {
return 'There was an unexpected error. Please try again.';
} else {
return $error ?: $message;
}
}
?>

View File

@ -5,7 +5,7 @@ require '../app/classes/component.php';
// connect to database
require '../app/helpers/database.php';
$db = connectDB($config, 'jilo');
$db = connectDB($config, 'jilo', $platform_id);
// specify time range
include '../app/helpers/time_range.php';

View File

@ -5,7 +5,7 @@ require '../app/classes/conference.php';
// connect to database
require '../app/helpers/database.php';
$db = connectDB($config, 'jilo');
$db = connectDB($config, 'jilo', $platform_id);
// specify time range
include '../app/helpers/time_range.php';

View File

@ -4,9 +4,6 @@ require_once '../app/classes/database.php';
require '../app/classes/conference.php';
require '../app/classes/participant.php';
// by default we connect ot the first configured platform
$platform_id = $_REQUEST['platform'] ?? '0';
// connect to database
require '../app/helpers/database.php';
$db = connectDB($config, 'jilo', $platform_id);

View File

@ -57,7 +57,7 @@ try {
}
}
} catch (Exception $e) {
$error = $e->getMessage();
$error = getError('There was an unexpected error. Please try again.', $e->getMessage());
}
if (!empty($config['login_message'])) {

View File

@ -5,7 +5,7 @@ require '../app/classes/participant.php';
// connect to database
require '../app/helpers/database.php';
$db = connectDB($config, 'jilo');
$db = connectDB($config, 'jilo', $platform_id);
// specify time range
include '../app/helpers/time_range.php';

View File

@ -32,7 +32,7 @@ if ($config['registration_enabled'] === true) {
}
}
} catch (Exception $e) {
$error = $e->getMessage();
$error = getError('There was an unexpected error. Please try again.', $e->getMessage());
}
include '../app/templates/block-message.php';

View File

@ -87,6 +87,9 @@ if (isset($_SESSION['error'])) {
$error = $_SESSION['error'];
}
// by default we connect ot the first configured platform
$platform_id = $_REQUEST['platform'] ?? '0';
// page building
if (in_array($page, $allowed_urls)) {
// logout is a special case, as we can't use session vars for notices