exceptions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  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 TemplateLoadError(TemplateError):
  28. """Raised when a template fails to load."""
  29. pass
  30. class TemplateSyntaxError(TemplateError):
  31. """Raised when a Jinja2 template has syntax errors."""
  32. def __init__(self, template_id: str, errors: List[str]):
  33. self.template_id = template_id
  34. self.errors = errors
  35. msg = f"Jinja2 syntax errors in template '{template_id}':\n" + "\n".join(errors)
  36. super().__init__(msg)
  37. class TemplateValidationError(TemplateError):
  38. """Raised when template validation fails."""
  39. pass
  40. class TemplateRenderError(TemplateError):
  41. """Raised when template rendering fails."""
  42. pass
  43. class VariableError(BoilerplatesError):
  44. """Base exception for variable-related errors."""
  45. pass
  46. class VariableValidationError(VariableError):
  47. """Raised when variable validation fails."""
  48. def __init__(self, variable_name: str, message: str):
  49. self.variable_name = variable_name
  50. msg = f"Validation error for variable '{variable_name}': {message}"
  51. super().__init__(msg)
  52. class VariableTypeError(VariableError):
  53. """Raised when a variable has an incorrect type."""
  54. def __init__(self, variable_name: str, expected_type: str, actual_type: str):
  55. self.variable_name = variable_name
  56. self.expected_type = expected_type
  57. self.actual_type = actual_type
  58. msg = f"Type error for variable '{variable_name}': expected {expected_type}, got {actual_type}"
  59. super().__init__(msg)
  60. class LibraryError(BoilerplatesError):
  61. """Raised when library operations fail."""
  62. pass
  63. class ModuleError(BoilerplatesError):
  64. """Raised when module operations fail."""
  65. pass
  66. class ModuleNotFoundError(ModuleError):
  67. """Raised when a module cannot be found."""
  68. def __init__(self, module_name: str):
  69. self.module_name = module_name
  70. msg = f"Module '{module_name}' not found"
  71. super().__init__(msg)
  72. class ModuleLoadError(ModuleError):
  73. """Raised when a module fails to load."""
  74. pass
  75. class FileOperationError(BoilerplatesError):
  76. """Raised when file operations fail."""
  77. pass
  78. class RenderError(BoilerplatesError):
  79. """Raised when rendering operations fail."""
  80. pass
  81. class YAMLParseError(BoilerplatesError):
  82. """Raised when YAML parsing fails."""
  83. def __init__(self, file_path: str, original_error: Exception):
  84. self.file_path = file_path
  85. self.original_error = original_error
  86. msg = f"Failed to parse YAML file '{file_path}': {original_error}"
  87. super().__init__(msg)