diff --git a/config.php b/config.php index 441d1a5..7a9c641 100644 --- a/config.php +++ b/config.php @@ -37,13 +37,17 @@ load_env(); /** * Get a PDO connection to the database. */ -function get_db_connection(): ?PDO { +function get_db_connection() { static $pdo = null; if ($pdo !== null) { return $pdo; } + if (!class_exists('PDO')) { + return null; + } + $url = getenv('DATABASE_URL'); if (!$url) { return null; @@ -66,9 +70,10 @@ function get_db_connection(): ?PDO { $pdo = new PDO($dsn, $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_TIMEOUT => 3, ]); return $pdo; - } catch (PDOException $e) { + } catch (\Exception $e) { error_log("Database connection failed: " . $e->getMessage()); return null; } diff --git a/helpers.php b/helpers.php index 21f8045..d1b0147 100644 --- a/helpers.php +++ b/helpers.php @@ -38,7 +38,7 @@ function init_db() { } } } - } catch (PDOException $e) { + } catch (\Exception $e) { error_log("DB Init Error: " . $e->getMessage()); } } @@ -70,7 +70,7 @@ function load_recipes_local(): array { } } } - } catch (PDOException $e) { + } catch (\Exception $e) { error_log("Failed to load recipes from DB: " . $e->getMessage()); } }