exceptions.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """Custom exception classes for the boilerplates CLI.
  2. This module defines specific exception types for better error handling
  3. and diagnostics throughout the application.
  4. """
  5. from typing import Optional, List, Dict
  6. class BoilerplatesError(Exception):
  7. """Base exception for all boilerplates CLI errors."""
  8. pass
  9. class ConfigError(BoilerplatesError):
  10. """Raised when configuration operations fail."""
  11. pass
  12. class ConfigValidationError(ConfigError):
  13. """Raised when configuration validation fails."""
  14. pass
  15. class TemplateError(BoilerplatesError):
  16. """Base exception for template-related errors."""
  17. pass
  18. class TemplateNotFoundError(TemplateError):
  19. """Raised when a template cannot be found."""
  20. def __init__(self, template_id: str, module_name: Optional[str] = None):
  21. self.template_id = template_id
  22. self.module_name = module_name
  23. msg = f"Template '{template_id}' not found"
  24. if module_name:
  25. msg += f" in module '{module_name}'"
  26. super().__init__(msg)
  27. class DuplicateTemplateError(TemplateError):
  28. """Raised when duplicate template IDs are found within the same library."""
  29. def __init__(self, template_id: str, library_name: str):
  30. self.template_id = template_id
  31. self.library_name = library_name
  32. super().__init__(
  33. f"Duplicate template ID '{template_id}' found in library '{library_name}'. "
  34. f"Each template within a library must have a unique ID."
  35. )
  36. class TemplateLoadError(TemplateError):
  37. """Raised when a template fails to load."""
  38. pass
  39. class TemplateSyntaxError(TemplateError):
  40. """Raised when a Jinja2 template has syntax errors."""
  41. def __init__(self, template_id: str, errors: List[str]):
  42. self.template_id = template_id
  43. self.errors = errors
  44. msg = f"Jinja2 syntax errors in template '{template_id}':\n" + "\n".join(errors)
  45. super().__init__(msg)
  46. class TemplateValidationError(TemplateError):
  47. """Raised when template validation fails."""
  48. pass
  49. class TemplateRenderError(TemplateError):
  50. """Raised when template rendering fails."""
  51. def __init__(
  52. self,
  53. message: str,
  54. file_path: Optional[str] = None,
  55. line_number: Optional[int] = None,
  56. column: Optional[int] = None,
  57. context_lines: Optional[List[str]] = None,
  58. variable_context: Optional[Dict[str, str]] = None,
  59. suggestions: Optional[List[str]] = None,
  60. original_error: Optional[Exception] = None
  61. ):
  62. self.file_path = file_path
  63. self.line_number = line_number
  64. self.column = column
  65. self.context_lines = context_lines or []
  66. self.variable_context = variable_context or {}
  67. self.suggestions = suggestions or []
  68. self.original_error = original_error
  69. # Build enhanced error message
  70. parts = [message]
  71. if file_path:
  72. location = f"File: {file_path}"
  73. if line_number:
  74. location += f", Line: {line_number}"
  75. if column:
  76. location += f", Column: {column}"
  77. parts.append(location)
  78. super().__init__("\n".join(parts))
  79. class VariableError(BoilerplatesError):
  80. """Base exception for variable-related errors."""
  81. pass
  82. class VariableValidationError(VariableError):
  83. """Raised when variable validation fails."""
  84. def __init__(self, variable_name: str, message: str):
  85. self.variable_name = variable_name
  86. msg = f"Validation error for variable '{variable_name}': {message}"
  87. super().__init__(msg)
  88. class VariableTypeError(VariableError):
  89. """Raised when a variable has an incorrect type."""
  90. def __init__(self, variable_name: str, expected_type: str, actual_type: str):
  91. self.variable_name = variable_name
  92. self.expected_type = expected_type
  93. self.actual_type = actual_type
  94. msg = f"Type error for variable '{variable_name}': expected {expected_type}, got {actual_type}"
  95. super().__init__(msg)
  96. class LibraryError(BoilerplatesError):
  97. """Raised when library operations fail."""
  98. pass
  99. class ModuleError(BoilerplatesError):
  100. """Raised when module operations fail."""
  101. pass
  102. class ModuleNotFoundError(ModuleError):
  103. """Raised when a module cannot be found."""
  104. def __init__(self, module_name: str):
  105. self.module_name = module_name
  106. msg = f"Module '{module_name}' not found"
  107. super().__init__(msg)
  108. class ModuleLoadError(ModuleError):
  109. """Raised when a module fails to load."""
  110. pass
  111. class FileOperationError(BoilerplatesError):
  112. """Raised when file operations fail."""
  113. pass
  114. class RenderError(BoilerplatesError):
  115. """Raised when rendering operations fail."""
  116. pass
  117. class YAMLParseError(BoilerplatesError):
  118. """Raised when YAML parsing fails."""
  119. def __init__(self, file_path: str, original_error: Exception):
  120. self.file_path = file_path
  121. self.original_error = original_error
  122. msg = f"Failed to parse YAML file '{file_path}': {original_error}"
  123. super().__init__(msg)