Error Handling
Errors can be raised whenever needed inside a session, and will be automatically handled by the @controller_method decorator.
Atlas have built-in, raiseable exceptions that already covers common HTTP errors.
For custom exceptions, any exceptions that inherits the base HttpError class will be handled as service layer errors. HttpError-based classes support custom messages, which can be conveniently fetched via the Messages class.
Errors raised in the repository layer will be handled as repository layer errors.
Other exceptions will be handled as controller layer errors.
Example:
src/services/order_service.py
from typing import List, Type
from atlas.common import ModelMapper, Page, Pageable
from atlas.decorators import service_method
from atlas.exceptions import BadRequestError
from atlas.layers import Service, InputDTO, RetDTO
from common import Messages
from enums import OrderStatusEnum
from models import Order
from repositories import OrderRepository
class OrderService(Service[Order]):
def __init__(self) -> None:
super().__init__()
self.address_service = None
self.order_repository = OrderRepository()
@service_method
def create(self, input_dto_instance: InputDTO, ret_dto_type: Type[RetDTO] = None) -> RetDTO | Order:
order = ModelMapper.map_instance_to_type(input_dto_instance, Order)
order = self.order_repository.add(order)
if order.address:
if not order.customer:
raise BadRequestError(Messages.get("ORDER_CUSTOMER_REQUIRED_FOR_ASSIGNING_ADDRESS_ERROR"))
elif order.address not in order.customer.addresses:
raise BadRequestError(Messages.get("ADDRESS_NOT_ASSOCIATED_WITH_ORDER_CUSTOMER_ERROR"))
self.order_repository.flush()
return self.map_or_self(order, ret_dto_type)