exceptions.py 5.9 KB

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