diff --git a/updation_kartik/README.md b/updation_kartik/README.md new file mode 100644 index 00000000..fab2f0c8 --- /dev/null +++ b/updation_kartik/README.md @@ -0,0 +1,26 @@ +# Jarvis Application Improvement + +## Update: Enhanced Error Handling and Security + +This update improves the main Jarvis application (`Jarvis.py`) with: + +### Key Improvements: +1. **Robust Error Handling**: Added comprehensive try-catch blocks to prevent application crashes +2. **Logging System**: Integrated proper logging for debugging and monitoring +3. **Safe Secret Access**: Created `safe_get_secret()` function to handle missing configuration gracefully +4. **Modular Design**: Separated authenticated page building into dedicated function +5. **Fallback Navigation**: Provides minimal functionality if critical errors occur + +### Security Enhancements: +- Prevents application crashes from configuration issues +- Logs security-related errors without exposing sensitive information +- Graceful degradation when secrets are missing + +### Usage: +Replace the current `Jarvis.py` with `improved_main_app.py` to implement these improvements. + +### Benefits: +- Better user experience with informative error messages +- Easier debugging with structured logging +- More resilient application that handles edge cases +- Improved security through safer secret handling \ No newline at end of file diff --git a/updation_kartik/improved_main_app.py b/updation_kartik/improved_main_app.py new file mode 100644 index 00000000..2d748625 --- /dev/null +++ b/updation_kartik/improved_main_app.py @@ -0,0 +1,96 @@ +import streamlit as st +import logging +from typing import Dict, Any + +from src.helpers.getFolders import getFolders +from src.helpers.structPages import structPages + +# Configure logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def safe_get_secret(key_path: str, default: Any = None) -> Any: + """Safely retrieve secrets with proper error handling.""" + try: + keys = key_path.split('.') + value = st.secrets + for key in keys: + value = value[key] + return value + except (KeyError, AttributeError) as e: + logger.warning(f"Secret key '{key_path}' not found: {e}") + return default + + +def build_authenticated_pages() -> Dict[str, Any]: + """Build pages for authenticated users with error handling.""" + pages = {} + + try: + MAIN_DIR = "src/apps/pages" + folders = getFolders(MAIN_DIR) + + if folders: + for folder_name, folder_dir in folders.items(): + try: + pages[folder_name.title()] = structPages(f"{MAIN_DIR}/{folder_dir}") + except Exception as e: + logger.error(f"Error loading pages for {folder_name}: {e}") + st.error(f"Failed to load {folder_name} section") + + # Add admin pages if user is admin + admin_email = safe_get_secret("general.ADMIN_EMAIL") + admin_name = safe_get_secret("general.ADMIN_NAME") + + if (admin_email and admin_name and + st.user.email == admin_email and + st.user.given_name == admin_name): + pages["Admin"] = [ + st.Page("src/apps/auth/env.py", title="Environment Variables", icon=":material/security:") + ] + + except Exception as e: + logger.error(f"Error building authenticated pages: {e}") + st.error("Failed to load some application sections") + + return pages + + +def application(): + """Main application with improved error handling.""" + try: + pages = { + "": [ + st.Page("src/apps/public/home.py", title="Home", icon=":material/home:"), + st.Page("src/apps/public/youtubePlaylist.py", title="Jarvis Videos", icon=":material/ondemand_video:"), + ], + "Account": [ + st.Page("src/apps/auth/auth.py", title="Authentication", icon=":material/lock_open:"), + ], + } + + # Add authenticated user pages + if st.user and st.user.is_logged_in: + authenticated_pages = build_authenticated_pages() + pages.update(authenticated_pages) + + return st.navigation(pages) + + except Exception as e: + logger.error(f"Critical error in application setup: {e}") + st.error("Application failed to initialize properly. Please refresh the page.") + # Return minimal navigation as fallback + return st.navigation({ + "": [st.Page("src/apps/public/home.py", title="Home", icon=":material/home:")] + }) + + +if __name__ == "__main__": + try: + app = application() + if app: + app.run() + except Exception as e: + logger.critical(f"Failed to start application: {e}") + st.error("Critical application error. Please contact support.") \ No newline at end of file