visini / abstracting-fastapi-services

FastAPI application and service structure for a more maintainable codebase – See full article
https://camillovisini.com/coding/abstracting-fastapi-services
MIT License
386 stars 54 forks source link

Why to use "with" statement for raising errors? #7

Open david-shiko opened 2 years ago

david-shiko commented 2 years ago

https://github.com/visini/abstracting-fastapi-services/blob/adacaba43609a0f46d1baf315d0288ae1a6c3d33/app/utils/service_result.py#L41

JohannSuarez commented 2 years ago

I don't understand either. It didn't come across as obvious to me that we had to use a context manager for this case. We even only have 'pass' in the exit( ) method.

lucasdepetrisd commented 2 weeks ago

The use of with is more beneficial when dealing with resources that need to be properly managed (like files or network connections), in this case, for simplicity, it would be better to remove it.


def handle_result(result: ServiceResult):
    if not result.success:
        logger.error(f"{result.value} | caller={caller_info()}")
        raise result.value
    return result.value```