Add custom exceptions handling
This commit is contained in:
157
app/exceptions/shop.py
Normal file
157
app/exceptions/shop.py
Normal file
@@ -0,0 +1,157 @@
|
||||
# app/exceptions/shop.py
|
||||
"""
|
||||
Shop management specific exceptions.
|
||||
"""
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
from .base import (
|
||||
ResourceNotFoundException,
|
||||
ConflictException,
|
||||
ValidationException,
|
||||
AuthorizationException,
|
||||
BusinessLogicException
|
||||
)
|
||||
|
||||
|
||||
class ShopNotFoundException(ResourceNotFoundException):
|
||||
"""Raised when a shop is not found."""
|
||||
|
||||
def __init__(self, shop_identifier: str, identifier_type: str = "code"):
|
||||
if identifier_type.lower() == "id":
|
||||
message = f"Shop with ID '{shop_identifier}' not found"
|
||||
else:
|
||||
message = f"Shop with code '{shop_identifier}' not found"
|
||||
|
||||
super().__init__(
|
||||
resource_type="Shop",
|
||||
identifier=shop_identifier,
|
||||
message=message,
|
||||
error_code="SHOP_NOT_FOUND",
|
||||
)
|
||||
|
||||
|
||||
class ShopAlreadyExistsException(ConflictException):
|
||||
"""Raised when trying to create a shop that already exists."""
|
||||
|
||||
def __init__(self, shop_code: str):
|
||||
super().__init__(
|
||||
message=f"Shop with code '{shop_code}' already exists",
|
||||
error_code="SHOP_ALREADY_EXISTS",
|
||||
details={"shop_code": shop_code},
|
||||
)
|
||||
|
||||
|
||||
class ShopNotActiveException(BusinessLogicException):
|
||||
"""Raised when trying to perform operations on inactive shop."""
|
||||
|
||||
def __init__(self, shop_code: str):
|
||||
super().__init__(
|
||||
message=f"Shop '{shop_code}' is not active",
|
||||
error_code="SHOP_NOT_ACTIVE",
|
||||
details={"shop_code": shop_code},
|
||||
)
|
||||
|
||||
|
||||
class ShopNotVerifiedException(BusinessLogicException):
|
||||
"""Raised when trying to perform operations requiring verified shop."""
|
||||
|
||||
def __init__(self, shop_code: str):
|
||||
super().__init__(
|
||||
message=f"Shop '{shop_code}' is not verified",
|
||||
error_code="SHOP_NOT_VERIFIED",
|
||||
details={"shop_code": shop_code},
|
||||
)
|
||||
|
||||
|
||||
class UnauthorizedShopAccessException(AuthorizationException):
|
||||
"""Raised when user tries to access shop they don't own."""
|
||||
|
||||
def __init__(self, shop_code: str, user_id: Optional[int] = None):
|
||||
details = {"shop_code": shop_code}
|
||||
if user_id:
|
||||
details["user_id"] = user_id
|
||||
|
||||
super().__init__(
|
||||
message=f"Unauthorized access to shop '{shop_code}'",
|
||||
error_code="UNAUTHORIZED_SHOP_ACCESS",
|
||||
details=details,
|
||||
)
|
||||
|
||||
|
||||
class InvalidShopDataException(ValidationException):
|
||||
"""Raised when shop data is invalid."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str = "Invalid shop data",
|
||||
field: Optional[str] = None,
|
||||
details: Optional[Dict[str, Any]] = None,
|
||||
):
|
||||
super().__init__(
|
||||
message=message,
|
||||
field=field,
|
||||
details=details,
|
||||
)
|
||||
self.error_code = "INVALID_SHOP_DATA"
|
||||
|
||||
|
||||
class ShopProductAlreadyExistsException(ConflictException):
|
||||
"""Raised when trying to add a product that already exists in shop."""
|
||||
|
||||
def __init__(self, shop_code: str, product_id: str):
|
||||
super().__init__(
|
||||
message=f"Product '{product_id}' already exists in shop '{shop_code}'",
|
||||
error_code="SHOP_PRODUCT_ALREADY_EXISTS",
|
||||
details={
|
||||
"shop_code": shop_code,
|
||||
"product_id": product_id,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class ShopProductNotFoundException(ResourceNotFoundException):
|
||||
"""Raised when a shop product relationship is not found."""
|
||||
|
||||
def __init__(self, shop_code: str, product_id: str):
|
||||
super().__init__(
|
||||
resource_type="ShopProduct",
|
||||
identifier=f"{shop_code}/{product_id}",
|
||||
message=f"Product '{product_id}' not found in shop '{shop_code}'",
|
||||
error_code="SHOP_PRODUCT_NOT_FOUND",
|
||||
)
|
||||
|
||||
|
||||
class MaxShopsReachedException(BusinessLogicException):
|
||||
"""Raised when user tries to create more shops than allowed."""
|
||||
|
||||
def __init__(self, max_shops: int, user_id: Optional[int] = None):
|
||||
details = {"max_shops": max_shops}
|
||||
if user_id:
|
||||
details["user_id"] = user_id
|
||||
|
||||
super().__init__(
|
||||
message=f"Maximum number of shops reached ({max_shops})",
|
||||
error_code="MAX_SHOPS_REACHED",
|
||||
details=details,
|
||||
)
|
||||
|
||||
|
||||
class ShopValidationException(ValidationException):
|
||||
"""Raised when shop validation fails."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str = "Shop validation failed",
|
||||
field: Optional[str] = None,
|
||||
validation_errors: Optional[Dict[str, str]] = None,
|
||||
):
|
||||
details = {}
|
||||
if validation_errors:
|
||||
details["validation_errors"] = validation_errors
|
||||
|
||||
super().__init__(
|
||||
message=message,
|
||||
field=field,
|
||||
details=details,
|
||||
)
|
||||
self.error_code = "SHOP_VALIDATION_FAILED"
|
||||
Reference in New Issue
Block a user