"""Custom exceptions and error handling for YouTrack CLI."""
from typing import Optional
__all__ = [
"YouTrackError",
"AuthenticationError",
"ConnectionError",
"ValidationError",
"NotFoundError",
"PermissionError",
"RateLimitError",
"YouTrackNetworkError",
"YouTrackServerError",
]
[docs]
class YouTrackError(Exception):
"""Base exception for YouTrack CLI errors."""
[docs]
def __init__(self, message: str, suggestion: Optional[str] = None):
super().__init__(message)
self.message = message
self.suggestion = suggestion
[docs]
class AuthenticationError(YouTrackError):
"""Authentication related errors."""
[docs]
def __init__(self, message: str = "Authentication failed"):
super().__init__(message, suggestion="Run 'yt auth login' to authenticate with YouTrack")
[docs]
class ConnectionError(YouTrackError):
"""Connection related errors."""
[docs]
def __init__(self, message: str = "Failed to connect to YouTrack"):
super().__init__(message, suggestion="Check your internet connection and YouTrack URL")
[docs]
class ValidationError(YouTrackError):
"""Input validation errors."""
[docs]
def __init__(self, message: str, field: Optional[str] = None):
if field:
message = f"Invalid {field}: {message}"
super().__init__(message)
self.field = field
[docs]
class NotFoundError(YouTrackError):
"""Resource not found errors."""
[docs]
def __init__(self, resource_type: str, identifier: str):
super().__init__(
f"{resource_type} '{identifier}' not found",
suggestion=(f"Check if the {resource_type.lower()} exists and you have access to it"),
)
[docs]
class PermissionError(YouTrackError):
"""Permission denied errors."""
[docs]
def __init__(self, action: str, resource: Optional[str] = None):
if resource:
message = f"Permission denied to {action} {resource}"
else:
message = f"Permission denied to {action}"
super().__init__(message, suggestion="Check your user permissions in YouTrack")
[docs]
class RateLimitError(YouTrackError):
"""Rate limit exceeded errors."""
[docs]
def __init__(self, retry_after: Optional[int] = None):
message = "Rate limit exceeded"
if retry_after:
message += f". Retry after {retry_after} seconds"
super().__init__(
message,
suggestion=("Wait a moment and try again, or reduce the frequency of requests"),
)
[docs]
class YouTrackNetworkError(YouTrackError):
"""Network related errors that may be retryable."""
[docs]
def __init__(self, message: str = "Network error occurred"):
super().__init__(message, suggestion="Check your internet connection and try again")
[docs]
class YouTrackServerError(YouTrackError):
"""Server-side errors that may be retryable."""
[docs]
def __init__(self, message: str = "Server error occurred", status_code: Optional[int] = None):
if status_code:
message = f"Server error (HTTP {status_code}): {message}"
super().__init__(message, suggestion="The server may be temporarily unavailable. Try again later")
self.status_code = status_code