exceptions.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """Custom exceptions for the boilerplate CLI application."""
  2. class BoilerplateError(Exception):
  3. """Base exception for all boilerplate-related errors."""
  4. pass
  5. class TemplateError(BoilerplateError):
  6. """Base exception for template-related errors."""
  7. pass
  8. class TemplateNotFoundError(TemplateError):
  9. """Raised when a template cannot be found."""
  10. def __init__(self, template_id: str, module_name: str = None):
  11. if module_name:
  12. message = f"Template '{template_id}' not found in module '{module_name}'"
  13. else:
  14. message = f"Template '{template_id}' not found"
  15. super().__init__(message)
  16. self.template_id = template_id
  17. self.module_name = module_name
  18. class InvalidTemplateError(TemplateError):
  19. """Raised when a template has invalid format or content."""
  20. def __init__(self, template_path: str, reason: str):
  21. message = f"Invalid template at '{template_path}': {reason}"
  22. super().__init__(message)
  23. self.template_path = template_path
  24. self.reason = reason
  25. class TemplateValidationError(TemplateError):
  26. """Raised when template validation fails."""
  27. def __init__(self, template_id: str, errors: list):
  28. message = f"Template '{template_id}' validation failed:\n" + "\n".join(f" - {e}" for e in errors)
  29. super().__init__(message)
  30. self.template_id = template_id
  31. self.errors = errors
  32. class VariableError(BoilerplateError):
  33. """Base exception for variable-related errors."""
  34. pass
  35. class UndefinedVariableError(VariableError):
  36. """Raised when a template references undefined variables."""
  37. def __init__(self, variable_names: set, template_id: str = None):
  38. var_list = ", ".join(sorted(variable_names))
  39. if template_id:
  40. message = f"Template '{template_id}' references undefined variables: {var_list}"
  41. else:
  42. message = f"Undefined variables: {var_list}"
  43. super().__init__(message)
  44. self.variable_names = variable_names
  45. self.template_id = template_id
  46. class LibraryError(BoilerplateError):
  47. """Base exception for library-related errors."""
  48. pass
  49. class RemoteLibraryError(LibraryError):
  50. """Raised when operations on remote libraries fail."""
  51. def __init__(self, library_name: str, operation: str, reason: str):
  52. message = f"Remote library '{library_name}' {operation} failed: {reason}"
  53. super().__init__(message)
  54. self.library_name = library_name
  55. self.operation = operation
  56. self.reason = reason
  57. class ConfigurationError(BoilerplateError):
  58. """Raised when configuration is invalid or missing."""
  59. def __init__(self, config_item: str, reason: str):
  60. message = f"Configuration error for '{config_item}': {reason}"
  61. super().__init__(message)
  62. self.config_item = config_item
  63. self.reason = reason