exceptions.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 IncompatibleSchemaVersionError(TemplateError):
  50. """Raised when a template uses a schema version not supported by the module."""
  51. def __init__(
  52. self,
  53. template_id: str,
  54. template_schema: str,
  55. module_schema: str,
  56. module_name: str,
  57. ):
  58. self.template_id = template_id
  59. self.template_schema = template_schema
  60. self.module_schema = module_schema
  61. self.module_name = module_name
  62. msg = (
  63. f"Template '{template_id}' uses schema version {template_schema}, "
  64. f"but module '{module_name}' only supports up to version {module_schema}.\n\n"
  65. f"This template requires features not available in your current CLI version.\n"
  66. f"Please upgrade the boilerplates CLI.\n\n"
  67. f"Run: pip install --upgrade boilerplates"
  68. )
  69. super().__init__(msg)
  70. class TemplateRenderError(TemplateError):
  71. """Raised when template rendering fails."""
  72. def __init__(
  73. self,
  74. message: str,
  75. file_path: Optional[str] = None,
  76. line_number: Optional[int] = None,
  77. column: Optional[int] = None,
  78. context_lines: Optional[List[str]] = None,
  79. variable_context: Optional[Dict[str, str]] = None,
  80. suggestions: Optional[List[str]] = None,
  81. original_error: Optional[Exception] = None,
  82. ):
  83. self.file_path = file_path
  84. self.line_number = line_number
  85. self.column = column
  86. self.context_lines = context_lines or []
  87. self.variable_context = variable_context or {}
  88. self.suggestions = suggestions or []
  89. self.original_error = original_error
  90. # Build enhanced error message
  91. parts = [message]
  92. if file_path:
  93. location = f"File: {file_path}"
  94. if line_number:
  95. location += f", Line: {line_number}"
  96. if column:
  97. location += f", Column: {column}"
  98. parts.append(location)
  99. super().__init__("\n".join(parts))
  100. class VariableError(BoilerplatesError):
  101. """Base exception for variable-related errors."""
  102. pass
  103. class VariableValidationError(VariableError):
  104. """Raised when variable validation fails."""
  105. def __init__(self, variable_name: str, message: str):
  106. self.variable_name = variable_name
  107. msg = f"Validation error for variable '{variable_name}': {message}"
  108. super().__init__(msg)
  109. class VariableTypeError(VariableError):
  110. """Raised when a variable has an incorrect type."""
  111. def __init__(self, variable_name: str, expected_type: str, actual_type: str):
  112. self.variable_name = variable_name
  113. self.expected_type = expected_type
  114. self.actual_type = actual_type
  115. msg = f"Type error for variable '{variable_name}': expected {expected_type}, got {actual_type}"
  116. super().__init__(msg)
  117. class LibraryError(BoilerplatesError):
  118. """Raised when library operations fail."""
  119. pass
  120. class ModuleError(BoilerplatesError):
  121. """Raised when module operations fail."""
  122. pass
  123. class ModuleNotFoundError(ModuleError):
  124. """Raised when a module cannot be found."""
  125. def __init__(self, module_name: str):
  126. self.module_name = module_name
  127. msg = f"Module '{module_name}' not found"
  128. super().__init__(msg)
  129. class ModuleLoadError(ModuleError):
  130. """Raised when a module fails to load."""
  131. pass
  132. class FileOperationError(BoilerplatesError):
  133. """Raised when file operations fail."""
  134. pass
  135. class RenderError(BoilerplatesError):
  136. """Raised when rendering operations fail."""
  137. pass
  138. class YAMLParseError(BoilerplatesError):
  139. """Raised when YAML parsing fails."""
  140. def __init__(self, file_path: str, original_error: Exception):
  141. self.file_path = file_path
  142. self.original_error = original_error
  143. msg = f"Failed to parse YAML file '{file_path}': {original_error}"
  144. super().__init__(msg)