Skip to content

fix: Improve error handling in CLI (Issue #35)#45

Open
AliiiBenn wants to merge 4 commits intomainfrom
improve-error-handling-35
Open

fix: Improve error handling in CLI (Issue #35)#45
AliiiBenn wants to merge 4 commits intomainfrom
improve-error-handling-35

Conversation

@AliiiBenn
Copy link
Member

@AliiiBenn AliiiBenn commented Jan 23, 2026

Summary

This PR fixes Issue #35 by implementing comprehensive error handling improvements throughout the CLI.

Problem

Poor error handling patterns:

  • Bare except: blocks
  • Generic except Exception: handlers
  • Inconsistent error message formats
  • No actionable user guidance

Solution

1. Custom Exception Hierarchy

Created excel_to_sql/exceptions.py:

  • ExcelToSqlError (base)
  • ExcelFileError - Excel operations
  • ConfigurationError - Configuration issues
  • ValidationError - Validation failures
  • DatabaseError - Database operations

2. Improved CLI Error Handling

Import Command:

  • FileNotFoundError → "File not found" + tip
  • PermissionError → "Permission denied" + tip
  • pd.errors.EmptyDataError → "Empty file" + tip
  • pd.errors.ParserError → "Invalid format" + tip

Export Command:

  • File/database errors with context

Magic Command:

  • Specific exception handling
  • Removed bare except:

Changes

  • excel_to_sql/exceptions.py (253 lines) - NEW
  • excel_to_sql/cli.py (+118/-8)
  • excel_to_sql/entities/excel_file.py (+49/-3)
  • tests/test_exceptions.py (289 lines) - NEW

Test 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

AliiiBenn and others added 4 commits January 23, 2026 14:57
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Error Handling in CLI

1 participant