From e5945e91a31d7c06e66c50f83495db3633360334 Mon Sep 17 00:00:00 2001 From: Greg Sarjeant Date: Tue, 5 Aug 2025 00:44:27 +0000 Subject: [PATCH] Add error handling to remaining controllers. (#56) Reviewed-on: https://gitea.subcultureofone.org/greg/tkr/pulls/56 Co-authored-by: Greg Sarjeant Co-committed-by: Greg Sarjeant --- .../AuthController/AuthController.php | 37 +++++++---- .../EmojiController/EmojiController.php | 32 ++++++++-- .../LogController/LogController.php | 64 +++++++++++++------ .../MoodController/MoodController.php | 28 +++++--- 4 files changed, 114 insertions(+), 47 deletions(-) diff --git a/src/Controller/AuthController/AuthController.php b/src/Controller/AuthController/AuthController.php index c5e5f1f..4d96001 100644 --- a/src/Controller/AuthController/AuthController.php +++ b/src/Controller/AuthController/AuthController.php @@ -23,21 +23,34 @@ class AuthController extends Controller { Log::debug("Login attempt for user {$username}"); - $userModel = new UserModel($app['db']); - $user = $userModel->getByUsername($username); + try { + $userModel = new UserModel($app['db']); + $user = $userModel->getByUsername($username); - //if ($user && password_verify($password, $user['password_hash'])) { - if ($user && password_verify($password, $user['password_hash'])) { - Log::info("Successful login for {$username}"); + if ($user && password_verify($password, $user['password_hash'])) { + Log::info("Successful login for {$username}"); - Session::newLoginSession($user); - header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); - exit; - } else { - Log::warning("Failed login for {$username}"); + try { + Session::newLoginSession($user); + header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); + exit; + } catch (Exception $e) { + Log::error("Failed to create login session for {$username}: " . $e->getMessage()); + Session::setFlashMessage('error', 'Login failed - session error'); + header('Location: ' . $_SERVER['PHP_SELF']); + exit; + } + } else { + Log::warning("Failed login for {$username}"); - // Set a flash message and reload the login page - Session::setFlashMessage('error', 'Invalid username or password'); + // Set a flash message and reload the login page + Session::setFlashMessage('error', 'Invalid username or password'); + header('Location: ' . $_SERVER['PHP_SELF']); + exit; + } + } catch (Exception $e) { + Log::error("Database error during login for {$username}: " . $e->getMessage()); + Session::setFlashMessage('error', 'Login temporarily unavailable'); header('Location: ' . $_SERVER['PHP_SELF']); exit; } diff --git a/src/Controller/EmojiController/EmojiController.php b/src/Controller/EmojiController/EmojiController.php index 3940cc0..be331e5 100644 --- a/src/Controller/EmojiController/EmojiController.php +++ b/src/Controller/EmojiController/EmojiController.php @@ -4,8 +4,14 @@ public function index(){ global $app; - $emojiModel = new EmojiModel($app['db']); - $emojiList = $emojiModel->getAll(); + try { + $emojiModel = new EmojiModel($app['db']); + $emojiList = $emojiModel->getAll(); + } catch (Exception $e) { + Log::error("Failed to load emoji list: " . $e->getMessage()); + $emojiList = []; + Session::setFlashMessage('error', 'Failed to load custom emoji'); + } $vars = [ 'config' => $app['config'], @@ -68,13 +74,19 @@ global $app; if (!$this->isValidEmoji($emoji)){ - // TODO - handle + Session::setFlashMessage('error', 'Invalid emoji format'); return; } // It looks like an emoji. Let's add it. - $emojiModel = new EmojiModel($app['db']); - $emojiList = $emojiModel->add($emoji, $description); + try { + $emojiModel = new EmojiModel($app['db']); + $emojiModel->add($emoji, $description); + Session::setFlashMessage('success', 'Emoji added successfully'); + } catch (Exception $e) { + Log::error("Failed to add emoji: " . $e->getMessage()); + Session::setFlashMessage('error', 'Failed to add emoji'); + } } public function handleDelete(): void { @@ -83,8 +95,14 @@ $ids = $_POST['delete_emoji_ids']; if (!empty($ids)) { - $emojiModel = new EmojiModel($app['db']); - $emojiModel->delete($ids); + try { + $emojiModel = new EmojiModel($app['db']); + $emojiModel->delete($ids); + Session::setFlashMessage('success', 'Emoji deleted successfully'); + } catch (Exception $e) { + Log::error("Failed to delete emoji: " . $e->getMessage()); + Session::setFlashMessage('error', 'Failed to delete emoji'); + } } } } \ No newline at end of file diff --git a/src/Controller/LogController/LogController.php b/src/Controller/LogController/LogController.php index 3bccfd5..d338843 100644 --- a/src/Controller/LogController/LogController.php +++ b/src/Controller/LogController/LogController.php @@ -30,12 +30,21 @@ class LogController extends Controller { $limit = 300; // Show last 300 log entries - // Read and parse log entries - $logEntries = $this->getLogEntries($limit, $levelFilter, $routeFilter); + try { + // Read and parse log entries + $logEntries = $this->getLogEntries($limit, $levelFilter, $routeFilter); - // Get available routes and levels for filter dropdowns - $availableRoutes = $this->getAvailableRoutes(); - $availableLevels = ['DEBUG', 'INFO', 'WARNING', 'ERROR']; + // Get available routes and levels for filter dropdowns + $availableRoutes = $this->getAvailableRoutes(); + $availableLevels = ['DEBUG', 'INFO', 'WARNING', 'ERROR']; + } catch (Exception $e) { + Log::error("Failed to load log data: " . $e->getMessage()); + // Provide empty data if log reading fails + $logEntries = []; + $availableRoutes = []; + $availableLevels = ['DEBUG', 'INFO', 'WARNING', 'ERROR']; + Session::setFlashMessage('error', 'Unable to load log files'); + } return [ 'config' => $app['config'], @@ -51,27 +60,42 @@ class LogController extends Controller { $logFile = $this->storageDir . '/logs/tkr.log'; $entries = []; - // Read from current log file and rotated files - $logFiles = [$logFile]; - for ($i = 1; $i <= 5; $i++) { - $rotatedFile = $logFile . '.' . $i; - if (file_exists($rotatedFile)) { - $logFiles[] = $rotatedFile; + try { + // Read from current log file and rotated files + $logFiles = [$logFile]; + for ($i = 1; $i <= 5; $i++) { + $rotatedFile = $logFile . '.' . $i; + if (file_exists($rotatedFile)) { + $logFiles[] = $rotatedFile; + } } - } - foreach ($logFiles as $file) { - if (file_exists($file)) { - $lines = file($file, FILE_IGNORE_NEW_LINES); - foreach (array_reverse($lines) as $line) { - if (count($entries) >= $limit) break 2; + foreach ($logFiles as $file) { + if (file_exists($file)) { + try { + $lines = file($file, FILE_IGNORE_NEW_LINES); + if ($lines === false) { + Log::warning("Failed to read log file: $file"); + continue; + } + + foreach (array_reverse($lines) as $line) { + if (count($entries) >= $limit) break 2; - $entry = $this->parseLogLine($line); - if ($entry && $this->matchesFilters($entry, $levelFilter, $routeFilter)) { - $entries[] = $entry; + $entry = $this->parseLogLine($line); + if ($entry && $this->matchesFilters($entry, $levelFilter, $routeFilter)) { + $entries[] = $entry; + } + } + } catch (Exception $e) { + Log::warning("Error reading log file $file: " . $e->getMessage()); + continue; } } } + } catch (Exception $e) { + Log::error("Failed to read log entries: " . $e->getMessage()); + // Return empty array if we can't read logs } return $entries; diff --git a/src/Controller/MoodController/MoodController.php b/src/Controller/MoodController/MoodController.php index 65b9303..a34e08a 100644 --- a/src/Controller/MoodController/MoodController.php +++ b/src/Controller/MoodController/MoodController.php @@ -29,8 +29,14 @@ } // set or clear the mood - $app['user']->mood = $mood; - $app['user'] = $app['user']->save(); + try { + $app['user']->mood = $mood; + $app['user'] = $app['user']->save(); + Session::setFlashMessage('success', 'Mood updated'); + } catch (Exception $e) { + Log::error("Failed to save mood: " . $e->getMessage()); + Session::setFlashMessage('error', 'Failed to update mood'); + } // go back to the index and show the updated mood header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); @@ -41,15 +47,21 @@ private static function getEmojisWithLabels(): array { global $app; - $emojiModel = new EmojiModel($app['db']); - $customEmoji = $emojiModel->getAll(); + try { + $emojiModel = new EmojiModel($app['db']); + $customEmoji = $emojiModel->getAll(); - if (!empty($customEmoji)){ - $custom = []; + if (!empty($customEmoji)){ + $custom = []; - foreach ($customEmoji as $item){ - $custom[] = [$item['emoji'], $item['description']]; + foreach ($customEmoji as $item){ + $custom[] = [$item['emoji'], $item['description']]; + } } + } catch (Exception $e) { + Log::error("Failed to load custom emoji: " . $e->getMessage()); + // Continue without custom emoji if database fails + $customEmoji = []; } $emoji = [