def error_to_alert( error:Any, # Error object (BaseError from cjm-error-handling or standard Exception) include_debug_info:bool=False, # Whether to include debug information in the alert)->FT: # Alert component (error or warning)
Convert an error to an alert component.
Example: Converting Errors to Alerts
# Example 1: Standard exceptiontry:raiseValueError("Invalid configuration value")exceptValueErroras e: alert_component = error_to_alert(e)print("Standard exception alert created")# Example 2: Structured error (if library available)if _has_error_handling:try:raise PluginError( message="Failed to load plugin", debug_info="Plugin class not found in module", context=ErrorContext(operation="load_plugin"), plugin_id="test_plugin" )except PluginError as e: alert_component = error_to_alert(e, include_debug_info=True)print("Structured error alert with debug info created")
Standard exception alert created
Structured error alert with debug info created
HTMX Error Response Utilities
Create HTMX-compatible error responses that can update specific page elements.
def error_to_htmx_response( error:Any, # Error object (BaseError or Exception) target_id:str='alert-container', # ID of element to update with error include_debug_info:bool=False, # Whether to include debug information)->FT: # Alert component with correct ID for HTMX targeting
Create an HTMX-compatible error response.
# Example: HTMX error responseif _has_error_handling: error = ValidationError( message="Invalid form data", debug_info="Field 'port' must be between 1-65535", context=ErrorContext(operation="validate_form"), validation_errors={"port": "Out of range"} ) htmx_response = error_to_htmx_response(error)print("HTMX error response created")
HTMX error response created
Error Page Components
Full-page error displays for critical failures or 404/500 errors.
def create_error_page( title:str='Error', # Page title message:str='An error occurred', # Main error message details:Optional=None, # Optional details or debug info show_home_link:bool=True, # Whether to show a link back to home home_path:str='/', # Path for the home link (defaults to root))->FT: # Div element containing the full error page
Create a full-page error display.
# Example: Create a 404 error pagecreate_error_page( title="Page Not Found", message="The page you're looking for doesn't exist", details="Error 404")
def error_to_page( error:Any, # Error object (BaseError or Exception) include_debug_info:bool=False, # Whether to include debug information show_home_link:bool=True, # Whether to show a link back to home home_path:str='/', # Path for the home link (defaults to root))->FT: # Full error page component