PythonNest / PyNest

PyNest is a Python framework built on top of FastAPI that follows the modular architecture of NestJS
https://pythonnest.github.io/PyNest
MIT License
643 stars 45 forks source link

return HTTPException in async_db_request_handler #67

Open nurlybek-dev opened 4 weeks ago

nurlybek-dev commented 4 weeks ago

Hello, Problem, when exception raised in service with async_db_request_handler decorator, it return HTTPException, which FastAPI can't serialize and raised another exception

fastapi.exceptions.ResponseValidationError: 1 validation errors:
{'loc': ('response',), 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}

code example: Controller

@Get("/")
async def get_accounts(session: AsyncSession = Depends(config.get_db)):
    return await self.service.get_list(session)

Service

@async_db_request_handler
async def get_list(self, session: AsyncSession):
    query = select(Entity)

    if True:
        raise ValueError("ERROR")

    result = await session.execute(query)
    return result.scalars().all()

Maybe it should be raise HTTPException instead of return HTTPException? code line nest/core/decorators/database.py:82.

ItayTheDar commented 4 weeks ago

@nurlybek-dev Hi!

I believe you are correct. When the service layer return an exception, then the controller does not know that an error was raised and it breaking the exceptions chain. I will work on pr that fix it.