From 24f16e454e5d5706d0427e211186aa1fe494d631 Mon Sep 17 00:00:00 2001 From: smueller Date: Tue, 7 Jul 2026 12:35:36 +0200 Subject: [PATCH] Enhance error handling in saveJsonFile function in index.php. Added checks for JSON encoding and file writing, throwing exceptions with descriptive messages for better debugging and user guidance. --- index.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index 52feab3..1d06380 100644 --- a/index.php +++ b/index.php @@ -76,11 +76,15 @@ function loadJsonFile(string $file, array $default): array function saveJsonFile(string $file, array $data): void { - file_put_contents( - $file, - json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), - LOCK_EX - ); + $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + if ($json === false) { + throw new RuntimeException('Konfiguration konnte nicht kodiert werden.'); + } + + $written = @file_put_contents($file, $json, LOCK_EX); + if ($written === false) { + throw new RuntimeException('Konfiguration konnte nicht gespeichert werden. Bitte Dateirechte prüfen.'); + } } function isAdminLoggedIn(): bool