PupaClic
feat(tests): Add validation and testing scripts for dashboard router error handling fix
5c22c89 | #!/usr/bin/env python3 | |
| """ | |
| Minimal validation of the dashboard router fix | |
| """ | |
| def validate_error_handling_fix(): | |
| """Validate that the error handling fix is properly implemented""" | |
| print("π Validating Dashboard Router Error Handling Fix") | |
| print("=" * 50) | |
| try: | |
| # Read the dashboard router file | |
| with open('app/routers/dashboard_router.py', 'r') as f: | |
| content = f.read() | |
| # Find the get_dashboard_layout function | |
| function_start = content.find("async def get_dashboard_layout(") | |
| if function_start == -1: | |
| print("β get_dashboard_layout function not found") | |
| return False | |
| # Extract the function content (rough estimation) | |
| function_content = content[function_start:content.find("\n@router.", function_start)] | |
| print("β Found get_dashboard_layout function") | |
| # Check for the original problematic pattern | |
| problematic_patterns = [ | |
| 'extra={"merchant_id": merchant_id, "role_id": role_id,"associate_id":associate_id}', | |
| 'extra={"merchant_id": merchant_id, "role_id": role_id, "associate_id": associate_id}' | |
| ] | |
| has_problematic = any(pattern in function_content for pattern in problematic_patterns) | |
| if has_problematic: | |
| print("β Still contains problematic direct variable access in error handler") | |
| return False | |
| else: | |
| print("β No direct variable access found in error handler") | |
| # Check for our fix patterns | |
| fix_patterns = [ | |
| "extra_data = {}", | |
| "if 'merchant_id' in locals():", | |
| "if 'role_id' in locals():", | |
| "if 'associate_id' in locals():" | |
| ] | |
| fix_found = all(pattern in function_content for pattern in fix_patterns) | |
| if fix_found: | |
| print("β All fix patterns found") | |
| else: | |
| print("β οΈ Some fix patterns missing") | |
| for pattern in fix_patterns: | |
| if pattern in function_content: | |
| print(f" β {pattern}") | |
| else: | |
| print(f" β {pattern}") | |
| # Check that we're using extra_data in the logger.error call | |
| if "extra=extra_data" in function_content: | |
| print("β Using safe extra_data in logger.error") | |
| else: | |
| print("β Not using safe extra_data in logger.error") | |
| return False | |
| print("\nπ Validation Summary:") | |
| print("β Original problematic pattern removed") | |
| print("β Safe variable checking implemented") | |
| print("β Protected error logging in place") | |
| return True | |
| except Exception as e: | |
| print(f"β Error validating fix: {e}") | |
| return False | |
| def show_before_after(): | |
| """Show what the fix changed""" | |
| print("\nπ What the Fix Changed:") | |
| print("=" * 25) | |
| print("BEFORE (problematic):") | |
| print("```python") | |
| print("except Exception as e:") | |
| print(" logger.error(") | |
| print(' "Error fetching dashboard layout",') | |
| print(' extra={"merchant_id": merchant_id, "role_id": role_id, "associate_id": associate_id},') | |
| print(" exc_info=e,") | |
| print(" )") | |
| print("```") | |
| print("\nAFTER (fixed):") | |
| print("```python") | |
| print("except Exception as e:") | |
| print(" # Use .get() to avoid NameError if variables aren't set yet") | |
| print(" extra_data = {}") | |
| print(" if 'merchant_id' in locals():") | |
| print(" extra_data['merchant_id'] = merchant_id") | |
| print(" if 'role_id' in locals():") | |
| print(" extra_data['role_id'] = role_id") | |
| print(" if 'associate_id' in locals():") | |
| print(" extra_data['associate_id'] = associate_id") | |
| print(" ") | |
| print(" logger.error(") | |
| print(' "Error fetching dashboard layout",') | |
| print(" extra=extra_data,") | |
| print(" exc_info=e,") | |
| print(" )") | |
| print("```") | |
| def main(): | |
| print("π Dashboard Router Fix Validation") | |
| print("Checking if the 500 error fix is properly implemented\n") | |
| if validate_error_handling_fix(): | |
| print("\nπ SUCCESS: The fix is properly implemented!") | |
| print("\nThe changes ensure that:") | |
| print("β’ Variables are checked for existence before use") | |
| print("β’ No NameError will occur in error handling") | |
| print("β’ 500 errors from undefined variables are prevented") | |
| print("β’ Proper error responses will be returned instead") | |
| else: | |
| print("\nβ FAILED: The fix may not be complete") | |
| show_before_after() | |
| print("\nπ Next Steps:") | |
| print("1. Start the service with: uvicorn app.app:app --port 8000") | |
| print("2. Test the endpoint to confirm 500 errors are resolved") | |
| print("3. Expected: 401/403 (auth) or 200 (success) instead of 500") | |
| if __name__ == "__main__": | |
| main() |