Problem: - Ruff removed 'from app.core.database import Base' from models/database/base.py - Import appeared "unused" (F401) but was actually a critical re-export - Caused ImportError: cannot import name 'Base' at runtime - Re-export pattern: import in one file to export from package Solution: 1. Added F401 ignore for models/database/base.py in pyproject.toml 2. Created scripts/verify_critical_imports.py verification script 3. Integrated verification into make check and CI pipeline 4. Updated documentation with explanation New Verification Script: - Checks all critical re-export imports exist - Detects import variations (parentheses, 'as' clauses) - Handles SQLAlchemy declarative_base alternatives - Runs as part of make check automatically Protected Files: - models/database/base.py - Re-exports Base for all models - models/__init__.py - Exports Base for Alembic - models/database/__init__.py - Exports Base from package - All __init__.py files (already protected) Makefile Changes: - make verify-imports - Run import verification - make check - Now includes verify-imports - make ci - Includes verify-imports in pipeline Documentation Updated: - Code quality guide explains re-export protection - Pre-commit workflow includes verification - Examples of why re-exports matter This prevents future issues where linters remove seemingly "unused" imports that are actually critical for application structure. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
95 lines
2.5 KiB
Python
95 lines
2.5 KiB
Python
# app/exceptions/auth.py
|
|
"""
|
|
Authentication and authorization specific exceptions.
|
|
"""
|
|
|
|
from .base import AuthenticationException, AuthorizationException, ConflictException
|
|
|
|
|
|
class InvalidCredentialsException(AuthenticationException):
|
|
"""Raised when login credentials are invalid."""
|
|
|
|
def __init__(self, message: str = "Invalid username or password"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="INVALID_CREDENTIALS",
|
|
)
|
|
|
|
|
|
class TokenExpiredException(AuthenticationException):
|
|
"""Raised when JWT token has expired."""
|
|
|
|
def __init__(self, message: str = "Token has expired"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="TOKEN_EXPIRED",
|
|
)
|
|
|
|
|
|
class InvalidTokenException(AuthenticationException):
|
|
"""Raised when JWT token is invalid or malformed."""
|
|
|
|
def __init__(self, message: str = "Invalid token"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="INVALID_TOKEN",
|
|
)
|
|
|
|
|
|
class InsufficientPermissionsException(AuthorizationException):
|
|
"""Raised when user lacks required permissions for an action."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str = "Insufficient permissions for this action",
|
|
required_permission: str | None = None,
|
|
):
|
|
details = {}
|
|
if required_permission:
|
|
details["required_permission"] = required_permission
|
|
|
|
super().__init__(
|
|
message=message,
|
|
error_code="INSUFFICIENT_PERMISSIONS",
|
|
details=details,
|
|
)
|
|
|
|
|
|
class UserNotActiveException(AuthorizationException):
|
|
"""Raised when user account is not active."""
|
|
|
|
def __init__(self, message: str = "User account is not active"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="USER_NOT_ACTIVE",
|
|
)
|
|
|
|
|
|
class AdminRequiredException(AuthorizationException):
|
|
"""Raised when admin privileges are required."""
|
|
|
|
def __init__(self, message: str = "Admin privileges required"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="ADMIN_REQUIRED",
|
|
)
|
|
|
|
|
|
class UserAlreadyExistsException(ConflictException):
|
|
"""Raised when trying to register with existing username/email."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str = "User already exists",
|
|
field: str | None = None,
|
|
):
|
|
details = {}
|
|
if field:
|
|
details["field"] = field
|
|
|
|
super().__init__(
|
|
message=message,
|
|
error_code="USER_ALREADY_EXISTS",
|
|
details=details,
|
|
)
|