Add error handling to remaining controllers. (#56)

Reviewed-on: https://gitea.subcultureofone.org/greg/tkr/pulls/56
Co-authored-by: Greg Sarjeant <greg@subcultureofone.org>
Co-committed-by: Greg Sarjeant <greg@subcultureofone.org>
This commit is contained in:
Greg Sarjeant 2025-08-05 00:44:27 +00:00 committed by greg
parent d9d0ed9571
commit e5945e91a3
4 changed files with 114 additions and 47 deletions

View File

@ -23,21 +23,34 @@ class AuthController extends Controller {
Log::debug("Login attempt for user {$username}"); Log::debug("Login attempt for user {$username}");
$userModel = new UserModel($app['db']); try {
$user = $userModel->getByUsername($username); $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'])) {
if ($user && password_verify($password, $user['password_hash'])) { Log::info("Successful login for {$username}");
Log::info("Successful login for {$username}");
Session::newLoginSession($user); try {
header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); Session::newLoginSession($user);
exit; header('Location: ' . Util::buildRelativeUrl($app['config']->basePath));
} else { exit;
Log::warning("Failed login for {$username}"); } 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 // Set a flash message and reload the login page
Session::setFlashMessage('error', 'Invalid username or password'); 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']); header('Location: ' . $_SERVER['PHP_SELF']);
exit; exit;
} }

View File

@ -4,8 +4,14 @@
public function index(){ public function index(){
global $app; global $app;
$emojiModel = new EmojiModel($app['db']); try {
$emojiList = $emojiModel->getAll(); $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 = [ $vars = [
'config' => $app['config'], 'config' => $app['config'],
@ -68,13 +74,19 @@
global $app; global $app;
if (!$this->isValidEmoji($emoji)){ if (!$this->isValidEmoji($emoji)){
// TODO - handle Session::setFlashMessage('error', 'Invalid emoji format');
return; return;
} }
// It looks like an emoji. Let's add it. // It looks like an emoji. Let's add it.
$emojiModel = new EmojiModel($app['db']); try {
$emojiList = $emojiModel->add($emoji, $description); $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 { public function handleDelete(): void {
@ -83,8 +95,14 @@
$ids = $_POST['delete_emoji_ids']; $ids = $_POST['delete_emoji_ids'];
if (!empty($ids)) { if (!empty($ids)) {
$emojiModel = new EmojiModel($app['db']); try {
$emojiModel->delete($ids); $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');
}
} }
} }
} }

View File

@ -30,12 +30,21 @@ class LogController extends Controller {
$limit = 300; // Show last 300 log entries $limit = 300; // Show last 300 log entries
// Read and parse log entries try {
$logEntries = $this->getLogEntries($limit, $levelFilter, $routeFilter); // Read and parse log entries
$logEntries = $this->getLogEntries($limit, $levelFilter, $routeFilter);
// Get available routes and levels for filter dropdowns // Get available routes and levels for filter dropdowns
$availableRoutes = $this->getAvailableRoutes(); $availableRoutes = $this->getAvailableRoutes();
$availableLevels = ['DEBUG', 'INFO', 'WARNING', 'ERROR']; $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 [ return [
'config' => $app['config'], 'config' => $app['config'],
@ -51,27 +60,42 @@ class LogController extends Controller {
$logFile = $this->storageDir . '/logs/tkr.log'; $logFile = $this->storageDir . '/logs/tkr.log';
$entries = []; $entries = [];
// Read from current log file and rotated files try {
$logFiles = [$logFile]; // Read from current log file and rotated files
for ($i = 1; $i <= 5; $i++) { $logFiles = [$logFile];
$rotatedFile = $logFile . '.' . $i; for ($i = 1; $i <= 5; $i++) {
if (file_exists($rotatedFile)) { $rotatedFile = $logFile . '.' . $i;
$logFiles[] = $rotatedFile; if (file_exists($rotatedFile)) {
$logFiles[] = $rotatedFile;
}
} }
}
foreach ($logFiles as $file) { foreach ($logFiles as $file) {
if (file_exists($file)) { if (file_exists($file)) {
$lines = file($file, FILE_IGNORE_NEW_LINES); try {
foreach (array_reverse($lines) as $line) { $lines = file($file, FILE_IGNORE_NEW_LINES);
if (count($entries) >= $limit) break 2; 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); $entry = $this->parseLogLine($line);
if ($entry && $this->matchesFilters($entry, $levelFilter, $routeFilter)) { if ($entry && $this->matchesFilters($entry, $levelFilter, $routeFilter)) {
$entries[] = $entry; $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; return $entries;

View File

@ -29,8 +29,14 @@
} }
// set or clear the mood // set or clear the mood
$app['user']->mood = $mood; try {
$app['user'] = $app['user']->save(); $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 // go back to the index and show the updated mood
header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); header('Location: ' . Util::buildRelativeUrl($app['config']->basePath));
@ -41,15 +47,21 @@
private static function getEmojisWithLabels(): array { private static function getEmojisWithLabels(): array {
global $app; global $app;
$emojiModel = new EmojiModel($app['db']); try {
$customEmoji = $emojiModel->getAll(); $emojiModel = new EmojiModel($app['db']);
$customEmoji = $emojiModel->getAll();
if (!empty($customEmoji)){ if (!empty($customEmoji)){
$custom = []; $custom = [];
foreach ($customEmoji as $item){ foreach ($customEmoji as $item){
$custom[] = [$item['emoji'], $item['description']]; $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 = [ $emoji = [