-
Notifications
You must be signed in to change notification settings - Fork 38
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
Pass exception to generators #113
Comments
i am try to work on this and will post my progress here. |
Problem Description:We have a generator function that provides a dependency at the beginning of the request lifecycle and finalizes it at the end of the request lifecycle. Inside the request lifecycle e.g: with container() as request_container:
... An exception may occur, the dependency is finalized anyway, but we also want to give it information about the exception that occurred by sending its object to the generator using the The codeprovider = Provider()
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
def __str__(self) -> str:
return f"I'm {self.name}, my age is: {self.age}"
def get_person() -> Iterator[Person]:
print("dependency initialization")
x = yield Person("alex", 18)
if isinstance(x, Exception):
print("Some exception occurred:", x)
print("dependency finalization")
provider.provide(get_person, scope=Scope.REQUEST)
container = make_container(provider)
with container() as request_container:
person = request_container.get(Person)
print("Hello!", person)
raise ValueError("Some error in request scope!") Expected outputdependency initialization
Hello! Im alex, my age is: 18
Some exception occurred: Some error in request scope!
dependency finalization
<Traceback here> Current outputdependency initialization
Hello! Im alex, my age is: 18
dependency finalization
<Traceback here> Correct me if I'm wrong. |
Implementation of this functionalityWhere do we need to make changes?In the case of Accordingly, we need to change What can we use to implement this functionality?We can easily see if an exception has occurred in the context manager block, because def __exit__(self, exc_type, exc_val, exc_tb): Where The finalization of the generators is handled by the def close(self) -> None:
errors = []
for exit_generator in self._exits[:::-1]:
try:
if exit_generator.type is FactoryType.GENERATOR:
next(exit_generator.callable) We could add an def close(self, exception: Optional[Exception] = None) -> None: Next, we just check for the argument in the loop and call if exit_generator.type is FactoryType.GENERATOR:
if not exception:
next(exit_generator.callable)
else:
exit_generator.callable.send(exception) The same is for the asynchronous version of Container, respectively. ConclusionIf you have no objections, I'll start to get down to implementation. |
Implement pass exception to generators #113
When we finalize dependencies we have no information if there were exception during process handling.
We can use
send
orasend
to pass it without breaking compatibility.Also we will need to add an optional parameter to
close
method ifContainer
object and modify it's__exit__
The text was updated successfully, but these errors were encountered: