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,16 +23,23 @@ class AuthController extends Controller {
Log::debug("Login attempt for user {$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}");
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}");
@ -41,6 +48,12 @@ class AuthController extends Controller {
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;
}
}
}

View File

@ -4,8 +4,14 @@
public function index(){
global $app;
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.
try {
$emojiModel = new EmojiModel($app['db']);
$emojiList = $emojiModel->add($emoji, $description);
$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)) {
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');
}
}
}
}

View File

@ -30,12 +30,21 @@ class LogController extends Controller {
$limit = 300; // Show last 300 log entries
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'];
} 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,6 +60,7 @@ class LogController extends Controller {
$logFile = $this->storageDir . '/logs/tkr.log';
$entries = [];
try {
// Read from current log file and rotated files
$logFiles = [$logFile];
for ($i = 1; $i <= 5; $i++) {
@ -62,7 +72,13 @@ class LogController extends Controller {
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;
@ -71,8 +87,16 @@ class LogController extends Controller {
$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;
}

View File

@ -29,8 +29,14 @@
}
// set or clear the mood
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,6 +47,7 @@
private static function getEmojisWithLabels(): array {
global $app;
try {
$emojiModel = new EmojiModel($app['db']);
$customEmoji = $emojiModel->getAll();
@ -51,6 +58,11 @@
$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 = [
'faces' => [