- Add proper media exceptions (MediaNotFoundException, MediaUploadException, etc.) - Update media service to use exceptions from app/exceptions/media - Remove direct HTTPException raises from vendor/media.py and vendor/customers.py - Move db.query from customers API to service layer (get_customer_orders) - Fix pagination macro call in vendor/media.html template - Update media.js: add parent init call, PlatformSettings, apiClient.postFormData - Add try/catch error handling to media.js init method 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
# app/exceptions/media.py
|
|
"""
|
|
Media/file management exceptions.
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
from .base import (
|
|
BusinessLogicException,
|
|
ResourceNotFoundException,
|
|
ValidationException,
|
|
)
|
|
|
|
|
|
class MediaNotFoundException(ResourceNotFoundException):
|
|
"""Raised when a media file is not found."""
|
|
|
|
def __init__(self, media_id: int | str):
|
|
super().__init__(
|
|
resource_type="MediaFile",
|
|
identifier=str(media_id),
|
|
message=f"Media file '{media_id}' not found",
|
|
error_code="MEDIA_NOT_FOUND",
|
|
)
|
|
|
|
|
|
class MediaUploadException(BusinessLogicException):
|
|
"""Raised when media upload fails."""
|
|
|
|
def __init__(self, message: str, details: dict[str, Any] | None = None):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="MEDIA_UPLOAD_FAILED",
|
|
details=details,
|
|
)
|
|
|
|
|
|
class MediaValidationException(ValidationException):
|
|
"""Raised when media validation fails (file type, size, etc.)."""
|
|
|
|
def __init__(
|
|
self,
|
|
message: str = "Media validation failed",
|
|
field: str | None = None,
|
|
details: dict[str, Any] | None = None,
|
|
):
|
|
super().__init__(message=message, field=field, details=details)
|
|
self.error_code = "MEDIA_VALIDATION_FAILED"
|
|
|
|
|
|
class UnsupportedMediaTypeException(ValidationException):
|
|
"""Raised when media file type is not supported."""
|
|
|
|
def __init__(self, file_type: str, allowed_types: list[str] | None = None):
|
|
details = {"file_type": file_type}
|
|
if allowed_types:
|
|
details["allowed_types"] = allowed_types
|
|
|
|
super().__init__(
|
|
message=f"Unsupported media type: {file_type}",
|
|
field="file",
|
|
details=details,
|
|
)
|
|
self.error_code = "UNSUPPORTED_MEDIA_TYPE"
|
|
|
|
|
|
class MediaFileTooLargeException(ValidationException):
|
|
"""Raised when media file exceeds size limit."""
|
|
|
|
def __init__(self, file_size: int, max_size: int, media_type: str = "file"):
|
|
super().__init__(
|
|
message=f"File size ({file_size} bytes) exceeds maximum allowed ({max_size} bytes) for {media_type}",
|
|
field="file",
|
|
details={
|
|
"file_size": file_size,
|
|
"max_size": max_size,
|
|
"media_type": media_type,
|
|
},
|
|
)
|
|
self.error_code = "MEDIA_FILE_TOO_LARGE"
|
|
|
|
|
|
class MediaOptimizationException(BusinessLogicException):
|
|
"""Raised when media optimization fails."""
|
|
|
|
def __init__(self, message: str = "Only images can be optimized"):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="MEDIA_OPTIMIZATION_FAILED",
|
|
)
|
|
|
|
|
|
class MediaDeleteException(BusinessLogicException):
|
|
"""Raised when media deletion fails."""
|
|
|
|
def __init__(self, message: str, details: dict[str, Any] | None = None):
|
|
super().__init__(
|
|
message=message,
|
|
error_code="MEDIA_DELETE_FAILED",
|
|
details=details,
|
|
)
|