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,16 +23,23 @@ class AuthController extends Controller { | |||||||
| 
 | 
 | ||||||
|             Log::debug("Login attempt for user {$username}"); |             Log::debug("Login attempt for user {$username}"); | ||||||
| 
 | 
 | ||||||
|  |             try { | ||||||
|                 $userModel = new UserModel($app['db']); |                 $userModel = new UserModel($app['db']); | ||||||
|                 $user = $userModel->getByUsername($username); |                 $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}"); | ||||||
| 
 | 
 | ||||||
|  |                     try { | ||||||
|                         Session::newLoginSession($user); |                         Session::newLoginSession($user); | ||||||
|                         header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); |                         header('Location: ' . Util::buildRelativeUrl($app['config']->basePath)); | ||||||
|                         exit; |                         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 { |                 } else { | ||||||
|                     Log::warning("Failed login for {$username}"); |                     Log::warning("Failed login for {$username}"); | ||||||
| 
 | 
 | ||||||
| @ -41,6 +48,12 @@ class AuthController extends Controller { | |||||||
|                     header('Location: ' . $_SERVER['PHP_SELF']); |                     header('Location: ' . $_SERVER['PHP_SELF']); | ||||||
|                     exit; |                     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(){ |         public function index(){ | ||||||
|             global $app; |             global $app; | ||||||
| 
 | 
 | ||||||
|  |             try { | ||||||
|                 $emojiModel = new EmojiModel($app['db']); |                 $emojiModel = new EmojiModel($app['db']); | ||||||
|                 $emojiList = $emojiModel->getAll(); |                 $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.
 | ||||||
|  |             try { | ||||||
|                 $emojiModel = new EmojiModel($app['db']); |                 $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 { |         public function handleDelete(): void { | ||||||
| @ -83,8 +95,14 @@ | |||||||
|             $ids = $_POST['delete_emoji_ids']; |             $ids = $_POST['delete_emoji_ids']; | ||||||
| 
 | 
 | ||||||
|             if (!empty($ids)) { |             if (!empty($ids)) { | ||||||
|  |                 try { | ||||||
|                     $emojiModel = new EmojiModel($app['db']); |                     $emojiModel = new EmojiModel($app['db']); | ||||||
|                     $emojiModel->delete($ids); |                     $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
 |         $limit = 300; // Show last 300 log entries
 | ||||||
| 
 | 
 | ||||||
|  |         try { | ||||||
|             // Read and parse log entries
 |             // Read and parse log entries
 | ||||||
|             $logEntries = $this->getLogEntries($limit, $levelFilter, $routeFilter); |             $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,6 +60,7 @@ class LogController extends Controller { | |||||||
|         $logFile = $this->storageDir . '/logs/tkr.log'; |         $logFile = $this->storageDir . '/logs/tkr.log'; | ||||||
|         $entries = []; |         $entries = []; | ||||||
| 
 | 
 | ||||||
|  |         try { | ||||||
|             // Read from current log file and rotated files
 |             // Read from current log file and rotated files
 | ||||||
|             $logFiles = [$logFile]; |             $logFiles = [$logFile]; | ||||||
|             for ($i = 1; $i <= 5; $i++) { |             for ($i = 1; $i <= 5; $i++) { | ||||||
| @ -62,7 +72,13 @@ class LogController extends Controller { | |||||||
| 
 | 
 | ||||||
|             foreach ($logFiles as $file) { |             foreach ($logFiles as $file) { | ||||||
|                 if (file_exists($file)) { |                 if (file_exists($file)) { | ||||||
|  |                     try { | ||||||
|                         $lines = file($file, FILE_IGNORE_NEW_LINES); |                         $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) { |                         foreach (array_reverse($lines) as $line) { | ||||||
|                             if (count($entries) >= $limit) break 2; |                             if (count($entries) >= $limit) break 2; | ||||||
| 
 | 
 | ||||||
| @ -71,8 +87,16 @@ class LogController extends Controller { | |||||||
|                                 $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; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -29,8 +29,14 @@ | |||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 // set or clear the mood
 |                 // set or clear the mood
 | ||||||
|  |                 try { | ||||||
|                     $app['user']->mood = $mood; |                     $app['user']->mood = $mood; | ||||||
|                     $app['user'] = $app['user']->save(); |                     $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,6 +47,7 @@ | |||||||
|         private static function getEmojisWithLabels(): array { |         private static function getEmojisWithLabels(): array { | ||||||
|             global $app; |             global $app; | ||||||
| 
 | 
 | ||||||
|  |             try { | ||||||
|                 $emojiModel = new EmojiModel($app['db']); |                 $emojiModel = new EmojiModel($app['db']); | ||||||
|                 $customEmoji = $emojiModel->getAll(); |                 $customEmoji = $emojiModel->getAll(); | ||||||
| 
 | 
 | ||||||
| @ -51,6 +58,11 @@ | |||||||
|                         $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 = [ | ||||||
|                 'faces' => [ |                 'faces' => [ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user