fix: Improve error handling in CLI (Issue #35)#45
Open
Conversation
…dling Implement structured exception hierarchy for excel-to-sql: - ExcelToSqlError (base) - All custom exceptions inherit from this - ExcelFileError - Excel file operation failures - ConfigurationError - Configuration issues - ValidationError - Data validation failures - DatabaseError - Database operation failures Features: - Context dictionary for additional error information - to_dict() method for serialization - Rich string representation with context details This enables better error handling, debugging, and user-friendly error messages throughout the application. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Update ExcelFile class to throw custom ExcelFileError instead of generic ValueError for better error handling: - read() - Throws ExcelFileError with file_path and operation context - read_all_sheets() - Specific error handling for empty/invalid files - read_sheets() - Wraps errors with ExcelFileError Improvements: - Distinguish between EmptyDataError (empty file) and ParserError (invalid format) - Include context (file_path, operation) for debugging - Preserve FileNotFoundError and PermissionError as-is - Chain original exceptions for full traceback This allows CLI to provide specific error messages and tips for common Excel file errors. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…able tips Replace generic exception handlers with specific exception types throughout the CLI: Import Command: - FileNotFoundError → "File not found" + tip to check path - PermissionError → "Permission denied" + tip to check permissions - EmptyDataError → "Empty Excel file" + tip to add data - ParserError → "Invalid Excel format" + tip to check file type - ConfigurationError → Config error + tip to check config files - ValidationError → Validation error with details - DatabaseError → Database error with context Export Command: - FileNotFoundError → "Table not found" + tip to import first - PermissionError → "Permission denied" + tip to check write access - DatabaseError → Database error with context Magic Command: - Improved error messages for file/sheet processing - Better exception handling in interactive mode quality reports - Replaced bare except: block with specific (AttributeError, TypeError) Status Command: - ConfigurationError for config-related failures Additional: - Added logger for unexpected errors - All error messages follow consistent format with tips - Debug mode shows full traceback on unexpected errors Fixes #35 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add 25 tests covering the custom exception hierarchy: ExcelToSqlError (base): - Base exception creation with and without context - to_dict() serialization ExcelFileError: - Creation with file_path and operation - Context dictionary inclusion - to_dict() serialization ConfigurationError: - Creation with config_file and config_key - Full context handling ValidationError: - Creation with field, value, and rule - Full context handling DatabaseError: - Creation with table, operation, and sql_error - Full context handling Exception Hierarchy: - All exceptions inherit from ExcelToSqlError - Base exception catches all custom types - Specific exception types can be caught individually - Exception chaining preserves original traceback All tests pass (25/25). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes Issue #35 by implementing comprehensive error handling improvements throughout the CLI.
Problem
Poor error handling patterns:
except:blocksexcept Exception:handlersSolution
1. Custom Exception Hierarchy
Created
excel_to_sql/exceptions.py:ExcelToSqlError(base)ExcelFileError- Excel operationsConfigurationError- Configuration issuesValidationError- Validation failuresDatabaseError- Database operations2. Improved CLI Error Handling
Import Command:
FileNotFoundError→ "File not found" + tipPermissionError→ "Permission denied" + tippd.errors.EmptyDataError→ "Empty file" + tippd.errors.ParserError→ "Invalid format" + tipExport Command:
Magic Command:
except:Changes
excel_to_sql/exceptions.py(253 lines) - NEWexcel_to_sql/cli.py(+118/-8)excel_to_sql/entities/excel_file.py(+49/-3)tests/test_exceptions.py(289 lines) - NEWTest Results
✅ 55 tests passing (exceptions + Excel file + database)
Acceptance Criteria
✅ P0: Removed bare
except:, specific exceptions, tips✅ P1: Custom exceptions, error context, consistent format
Breaking Changes
None.
Fixes #35
Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com