From d71bbd7ad6180387c65e2f668945f6bec81d856f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 20 May 2025 14:42:12 +0200 Subject: [inc/Database] Check if ErrorHandler is available before calling it --- inc/database.inc.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/inc/database.inc.php b/inc/database.inc.php index f928b7f5..278fd8f0 100644 --- a/inc/database.inc.php +++ b/inc/database.inc.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use JetBrains\PhpStorm\NoReturn; + /** * Handle communication with the database * This is a very thin layer between you and PDO. @@ -221,7 +223,7 @@ class Database self::$lastError = implode("\n", $stmt->errorInfo()); if ($ignoreError === true || ($ignoreError === null && self::$returnErrors)) return false; - ErrorHandler::traceError("Database Error: \n" . self::$lastError); + self::traceError("Database Error: \n" . self::$lastError); } if (CONFIG_DEBUG) { $duration = microtime(true) - $start; @@ -239,7 +241,26 @@ class Database self::$lastError = '(' . $e->getCode() . ') ' . $e->getMessage(); if ($ignoreError === true || ($ignoreError === null && self::$returnErrors)) return false; - ErrorHandler::traceError("Database Error: \n" . self::$lastError); + self::traceError("Database Error: \n" . self::$lastError); + } + } + + /** + * Wrapper around ErrorHandler::traceError as the autoloader might not + * be available at all times. + */ + #[NoReturn] + private static function traceError(string $message): void + { + if (class_exists('ErrorHandler')) { + ErrorHandler::traceError($message); + } else { + error_log($message); + foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $i => $e) { + error_log("#$i {$e['file']}:{$e['line']} ({$e['function']})"); + } + http_response_code(500); + exit(1); } } -- cgit v1.2.3-55-g7522