chore: add module exceptions, locales, and fix architecture warnings
- Create module-specific exceptions for cart, catalog, checkout - Add locales (en, de, fr, lb) for cart, catalog, checkout modules - Add missing lb.json for existing module locales - Add noqa comments for legitimate MOD-004 violations (core services) - Fix validator to use correct lb.json locale code (was lu.json) - Add noqa support for MOD-004 rule in validator Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
131
app/modules/catalog/exceptions.py
Normal file
131
app/modules/catalog/exceptions.py
Normal file
@@ -0,0 +1,131 @@
|
||||
# app/modules/catalog/exceptions.py
|
||||
"""
|
||||
Catalog module exceptions.
|
||||
|
||||
Module-specific exceptions for product catalog operations.
|
||||
"""
|
||||
|
||||
from app.exceptions.base import (
|
||||
BusinessLogicException,
|
||||
ConflictException,
|
||||
ResourceNotFoundException,
|
||||
ValidationException,
|
||||
)
|
||||
|
||||
|
||||
class ProductNotFoundException(ResourceNotFoundException):
|
||||
"""Raised when a product is not found in vendor catalog."""
|
||||
|
||||
def __init__(self, product_id: int, vendor_id: int | None = None):
|
||||
if vendor_id:
|
||||
message = f"Product with ID '{product_id}' not found in vendor {vendor_id} catalog"
|
||||
else:
|
||||
message = f"Product with ID '{product_id}' not found"
|
||||
|
||||
super().__init__(
|
||||
resource_type="Product",
|
||||
identifier=str(product_id),
|
||||
message=message,
|
||||
error_code="PRODUCT_NOT_FOUND",
|
||||
)
|
||||
self.details["product_id"] = product_id
|
||||
if vendor_id:
|
||||
self.details["vendor_id"] = vendor_id
|
||||
|
||||
|
||||
class ProductAlreadyExistsException(ConflictException):
|
||||
"""Raised when trying to add a product that already exists."""
|
||||
|
||||
def __init__(self, vendor_id: int, identifier: str | int):
|
||||
super().__init__(
|
||||
message=f"Product '{identifier}' already exists in vendor {vendor_id} catalog",
|
||||
error_code="PRODUCT_ALREADY_EXISTS",
|
||||
details={
|
||||
"vendor_id": vendor_id,
|
||||
"identifier": identifier,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class ProductNotInCatalogException(ResourceNotFoundException):
|
||||
"""Raised when trying to access a product that's not in vendor's catalog."""
|
||||
|
||||
def __init__(self, product_id: int, vendor_id: int):
|
||||
super().__init__(
|
||||
resource_type="Product",
|
||||
identifier=str(product_id),
|
||||
message=f"Product {product_id} is not in vendor {vendor_id} catalog",
|
||||
error_code="PRODUCT_NOT_IN_CATALOG",
|
||||
details={
|
||||
"product_id": product_id,
|
||||
"vendor_id": vendor_id,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class ProductNotActiveException(BusinessLogicException):
|
||||
"""Raised when trying to perform operations on inactive product."""
|
||||
|
||||
def __init__(self, product_id: int, vendor_id: int):
|
||||
super().__init__(
|
||||
message=f"Product {product_id} in vendor {vendor_id} catalog is not active",
|
||||
error_code="PRODUCT_NOT_ACTIVE",
|
||||
details={
|
||||
"product_id": product_id,
|
||||
"vendor_id": vendor_id,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class ProductValidationException(ValidationException):
|
||||
"""Raised when product data validation fails."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str = "Product validation failed",
|
||||
field: str | None = None,
|
||||
validation_errors: dict | None = None,
|
||||
):
|
||||
details = {}
|
||||
if validation_errors:
|
||||
details["validation_errors"] = validation_errors
|
||||
|
||||
super().__init__(
|
||||
message=message,
|
||||
field=field,
|
||||
details=details,
|
||||
)
|
||||
self.error_code = "PRODUCT_VALIDATION_FAILED"
|
||||
|
||||
|
||||
class CannotDeleteProductException(BusinessLogicException):
|
||||
"""Raised when a product cannot be deleted due to dependencies."""
|
||||
|
||||
def __init__(self, product_id: int, reason: str, details: dict | None = None):
|
||||
super().__init__(
|
||||
message=f"Cannot delete product {product_id}: {reason}",
|
||||
error_code="CANNOT_DELETE_PRODUCT",
|
||||
details={"product_id": product_id, "reason": reason, **(details or {})},
|
||||
)
|
||||
|
||||
|
||||
class ProductMediaException(BusinessLogicException):
|
||||
"""Raised when there's an issue with product media."""
|
||||
|
||||
def __init__(self, product_id: int, message: str):
|
||||
super().__init__(
|
||||
message=message,
|
||||
error_code="PRODUCT_MEDIA_ERROR",
|
||||
details={"product_id": product_id},
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CannotDeleteProductException",
|
||||
"ProductAlreadyExistsException",
|
||||
"ProductMediaException",
|
||||
"ProductNotActiveException",
|
||||
"ProductNotFoundException",
|
||||
"ProductNotInCatalogException",
|
||||
"ProductValidationException",
|
||||
]
|
||||
Reference in New Issue
Block a user