refactor: Standardize error handling patterns across codebase

Comprehensive error handling improvements addressing inconsistent patterns:

• Created markitect/exceptions.py with complete domain-specific exception hierarchy
  - MarkitectError base class with context and cause chaining support
  - Specific exceptions for Document, AST, Cache, Database, Schema operations
  - Built-in logging and context preservation

• Fixed overly broad exception handling in tddai modules:
  - issue_fetcher.py: Replace generic Exception with specific Gitea errors
  - project_manager.py: Proper error translation with context preservation
  - coverage_analyzer.py: Replace silent suppression with logging

• Enhanced cache_service.py error handling:
  - Specific OSError/PermissionError handling for file operations
  - Logging integration for unexpected errors
  - Preserved error collection and reporting

• Implemented proper exception chaining patterns:
  - All error translations use `raise ... from e` for debugging
  - Preserved original exception context and stack traces
  - Added docstring declarations of raised exceptions

• Benefits:
  - Eliminates silent error suppression and debugging black holes
  - Provides specific, actionable error messages
  - Preserves full error context for troubleshooting
  - Establishes consistent patterns for future development

Resolves issue #21: Error handling standardization

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-26 16:35:13 +02:00
parent 7f5309c4b0
commit bbc6192fe1
7 changed files with 420 additions and 18 deletions

View File

@@ -162,8 +162,15 @@ class CacheDirectoryService:
try:
cache_file.unlink()
removed_count += 1
except Exception as e:
except (OSError, PermissionError) as e:
errors.append(f"Could not remove {cache_file}: {e}")
except Exception as e:
# Log unexpected errors but continue cleanup
import logging
logging.getLogger(__name__).warning(
f"Unexpected error removing cache file {cache_file}: {e}"
)
errors.append(f"Unexpected error removing {cache_file}: {e}")
if errors:
return {
@@ -212,10 +219,22 @@ class CacheDirectoryService:
'file_removed': True,
'cache_file': str(cache_file)
}
except Exception as e:
except (OSError, PermissionError) as e:
return {
'success': False,
'message': f'Error removing cache for {source_path.name}: {e}',
'message': f'File system error removing cache for {source_path.name}: {e}',
'file_removed': False,
'error': str(e)
}
except Exception as e:
import logging
logging.getLogger(__name__).error(
f"Unexpected error removing cache for {source_path.name}: {e}",
exc_info=True
)
return {
'success': False,
'message': f'Unexpected error removing cache for {source_path.name}: {e}',
'file_removed': False,
'error': str(e)
}