Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle exceptions #409

Closed
wackazong opened this issue Feb 27, 2021 · 8 comments
Closed

How to handle exceptions #409

wackazong opened this issue Feb 27, 2021 · 8 comments
Assignees
Labels

Comments

@wackazong
Copy link

wackazong commented Feb 27, 2021

I have a fastapi example running with your library, I like the clean architecture. However, what is the best way to handle exceptions during dependency resolution? At the moment my FastAPI hangs and does not even return a 500.

@rmk135 rmk135 self-assigned this Feb 27, 2021
@rmk135
Copy link
Member

rmk135 commented Feb 27, 2021

Could you, please, attach a code sample?

@rmk135
Copy link
Member

rmk135 commented Feb 27, 2021

Also could you, please, check the version of dependency injector: pip list | grep dependency-injector?

@wackazong
Copy link
Author

I use poetry, dependency-injector is installed with 4.27.0. Here is my terminal output after doing a GET request to http://127.0.0.1:8000/listener/. The exception that I raise for testing is raised. I would expect that dependency-injector passes this exception to fastapi and fastapi handles it, resulting an a 500 being returned. Instead the request hangs.

% poetry run uvicorn api.main:app
INFO:     Started server process [22833]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Exception in callback Dependency._async_provide(<Future pendi...tasks.py:758]>)(<Future finis...n=Exception()>)
handle: <Handle Dependency._async_provide(<Future pendi...tasks.py:758]>)(<Future finis...n=Exception()>)>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "src/dependency_injector/providers.pyx", line 767, in dependency_injector.providers.Dependency._async_provide
  File "src/dependency_injector/providers.pyx", line 2652, in dependency_injector.providers.BaseSingleton._async_init_instance
  File "src/dependency_injector/providers.pxd", line 566, in dependency_injector.providers.__async_call_callback
  File "src/dependency_injector/providers.pxd", line 458, in dependency_injector.providers.__async_prepare_args_kwargs_callback
  File "src/dependency_injector/providers.pyx", line 3489, in dependency_injector.providers.Resource._async_init_callback
  File "./api/gateways/redis.py", line 6, in init_redis
    raise Exception
Exception

I put together a simple test case here: https://github.com/wackazong/dependency-injector_exception_handling_test_case. Many thanks for your support!

@rmk135
Copy link
Member

rmk135 commented Mar 1, 2021

Thank you for the traceback and test case. There were similar issues in asynchronous mode before. I thought I fixed all of them in 4.23.2 and 4.23.3, but looks like there are still some issues. I'll take a look and get back to you.

@rmk135 rmk135 removed the question label Mar 1, 2021
@rmk135
Copy link
Member

rmk135 commented Mar 1, 2021

@wackazong Fixed in 4.28.1. Thanks for reporting the issue and providing the test case.

@rmk135 rmk135 closed this as completed Mar 1, 2021
@wackazong
Copy link
Author

Very cool. Thank you too.

@robmoore
Copy link

robmoore commented Aug 13, 2022

I believe I'm running into this issue. I have a Resource that calls an async init function. The function raises an exception and my app hangs indefinitely. I can create a separate ticket for this issue if desired but it seems to be similar to what @wackazong mentioned. I'm running with version 4.40.0.

@robmoore
Copy link

@rmk135 I can provide an example if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants