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:
parent
d9d0ed9571
commit
e5945e91a3
@ -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;
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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 = [
|
||||
|
Loading…
x
Reference in New Issue
Block a user