55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""Custom exception classes."""
|
|
|
|
from typing import Optional
|
|
|
|
|
|
class AppException(Exception):
|
|
"""Base application exception."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str,
|
|
status_code: int = 500,
|
|
error_code: Optional[str] = None
|
|
):
|
|
self.message = message
|
|
self.status_code = status_code
|
|
self.error_code = error_code
|
|
super().__init__(self.message)
|
|
|
|
|
|
class NotFoundException(AppException):
|
|
"""Resource not found exception."""
|
|
|
|
def __init__(self, message: str = "Resource not found"):
|
|
super().__init__(message, status_code=404, error_code="NOT_FOUND")
|
|
|
|
|
|
class UnauthorizedException(AppException):
|
|
"""Authentication/authorization exception."""
|
|
|
|
def __init__(self, message: str = "Unauthorized access"):
|
|
super().__init__(message, status_code=401, error_code="UNAUTHORIZED")
|
|
|
|
|
|
class ForbiddenException(AppException):
|
|
"""Permission denied exception."""
|
|
|
|
def __init__(self, message: str = "Permission denied"):
|
|
super().__init__(message, status_code=403, error_code="FORBIDDEN")
|
|
|
|
|
|
class ValidationException(AppException):
|
|
"""Validation error exception."""
|
|
|
|
def __init__(self, message: str, details: Optional[list] = None):
|
|
super().__init__(message, status_code=422, error_code="VALIDATION_ERROR")
|
|
self.details = details or []
|
|
|
|
|
|
class ConflictException(AppException):
|
|
"""Resource conflict exception."""
|
|
|
|
def __init__(self, message: str = "Resource already exists"):
|
|
super().__init__(message, status_code=409, error_code="CONFLICT")
|