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 <?php
require '../app/helpers/errors.php';
class Database { class Database {
private $pdo; private $pdo;
public function __construct($options) { public function __construct($options) {
// pdo needed // pdo needed
if ( !extension_loaded('pdo') ) { if ( !extension_loaded('pdo') ) {
throw new Exception('PDO extension not loaded.'); $error = getError('PDO extension not loaded.');
} }
// options check // options check
if (empty($options['type'])) { if (empty($options['type'])) {
throw new Exception('Database type is not set.'); $error = getError('Database type is not set.');
} }
// database type // database type
@ -23,19 +25,19 @@ class Database {
$this->connectMysql($options); $this->connectMysql($options);
break; break;
default: default:
throw newException("Database type \"{$options['type']}\" is not supported."); $error = getError("Database type \"{$options['type']}\" is not supported.");
} }
} }
private function connectSqlite($options) { private function connectSqlite($options) {
// pdo_sqlite extension is needed // pdo_sqlite extension is needed
if (!extension_loaded('pdo_sqlite')) { if (!extension_loaded('pdo_sqlite')) {
throw new Exception('PDO extension for SQLite not loaded.'); $error = getError('PDO extension for SQLite not loaded.');
} }
// SQLite options // SQLite options
if (empty($options['dbFile']) || !file_exists($options['dbFile'])) { 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 // connect to SQLite
@ -43,19 +45,19 @@ class Database {
$this->pdo = new PDO("sqlite:" . $options['dbFile']); $this->pdo = new PDO("sqlite:" . $options['dbFile']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new Exception('SQLite connection failed: ' . $e->getMessage()); $error = getError('SQLite connection failed: ', $e->getMessage());
} }
} }
private function connectMysql($options) { private function connectMysql($options) {
// pdo_mysql extension is needed // pdo_mysql extension is needed
if (!extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo_mysql')) {
throw new Exception('PDO extension for MySQL not loaded.'); $error = getError('PDO extension for MySQL not loaded.');
} }
// MySQL options // MySQL options
if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) { 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 // Connect to MySQL
@ -64,7 +66,7 @@ class Database {
$this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? ''); $this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? '');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { } 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 // system info
'version' => '0.1.1', 'version' => '0.1.1',
// development has verbose error messages, production has not // development has verbose error messages, production has not
'environment' => 'production', 'environment' => 'production1',
// ************************************* // *************************************
// Maintained by the app, edit with care // Maintained by the app, edit with care

View File

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

View File

@ -5,7 +5,7 @@ require '../app/classes/conference.php';
// connect to database // connect to database
require '../app/helpers/database.php'; require '../app/helpers/database.php';
$db = connectDB($config, 'jilo'); $db = connectDB($config, 'jilo', $platform_id);
// specify time range // specify time range
include '../app/helpers/time_range.php'; 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/conference.php';
require '../app/classes/participant.php'; require '../app/classes/participant.php';
// by default we connect ot the first configured platform
$platform_id = $_REQUEST['platform'] ?? '0';
// connect to database // connect to database
require '../app/helpers/database.php'; require '../app/helpers/database.php';
$db = connectDB($config, 'jilo', $platform_id); $db = connectDB($config, 'jilo', $platform_id);

View File

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

View File

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

View File

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

View File

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