Repository navigation
fix: give error handlers an open container so @inject works in them - #62
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The DI middleware is an outer middleware on
dispatcher.update. aiogram's ownErrorsMiddlewareis registered there first, so it wraps ours. When a handler raises, our middleware closes the update's child container.ErrorsMiddlewarethen calls the error handlers with the samedatadict, which still holds the closed child. An@injecterror handler that resolves a REQUEST-scoped dependency raisedContainerClosedError, and that error replaced the handler's original exception.auto_inject=Truewraps error handlers too, so it had the same problem.The same middleware instance is now also an outer middleware on
dispatcher.errors. For anErrorEventit binds the failingUpdateand that update's event, so an error handler gets an open child of its own. Error handling also counts as in flight, so the shutdown wait covers it.The error handler gets a second child, not the failing update's. Its REQUEST-scoped instances are fresh, and the first child's finalizers have already run by then. Sharing the first child would mean inserting our middleware ahead of
ErrorsMiddleware, and aiogram'sMiddlewareManagerhas no public insert. Its list is private.The new test raises in a message handler and resolves a REQUEST dependency plus the event type in an
@injecterror handler. On main it fails withContainerClosedError. All 53 tests pass with 100% coverage.CONTEXT.mdnow describes the second child.