Skip to content

return HTTPException in async_db_request_handler #67

Closed

Description

@nurlybek-dev

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.

Activity

  1. ItayTheDar commented on Jun 11, 2024

    @ItayTheDar
    Contributor

    @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.

  2. ItayTheDar commented on Jul 28, 2026

    @ItayTheDar
    Contributor

    Resolved by the DI/runtime rewrite in #125. db_request_handler and async_db_request_handler now re-raise existing HTTPException instances and convert unexpected exceptions with raise HTTPException(...) rather than returning an exception object for response serialization.

    The original async failure pattern was reproduced against current main and correctly raises an HTTP 500 exception. Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions