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

Improve handle_errors context manager #657

Merged
merged 10 commits into from
Aug 10, 2023
Merged

Conversation

lukasjuhrich
Copy link
Collaborator

No description provided.

1. doing the abort() inside of `handle_errors` is useful, because
currently, every call of this context manager resides in a `try: …
except PycroftError: return default_response()`-block.

2. The `session.begin_nested()` seems convenient, but just hides
something important from  the person programming the view.
Since in the view, `session.commit()` has to be called explicitly as
well, it is only consistent to also call `session.begin_nested()`
explicitly.

Even more importantly, blindly wrapping the whole cm body in
`begin_nested` has two disadvantages:
a) It discourages „commit often“, which is favorable to long-running
transactions
b) It allows you to think `commit` inside this context will just work,
while this actually will cause problems in production (undetected in
tests, because there `commit()` is a NOP): sqlalchemy disallows
committing while in a subtransaction.

Thus, it makes more sense to let the user call `begin_nested`
explicitly.
This will be removed after fixing all the usages
This will not cause errors to be unhandled, because exiting out of the
`begin_nested()` context manager will cause a `flush()`, triggering
all of the possible errors that might occur in the transaction.
It has been replaced by the `handle_errors` context manager.
@lukasjuhrich lukasjuhrich marked this pull request as ready for review August 10, 2023 15:31
@lukasjuhrich lukasjuhrich merged commit 12d9d34 into develop Aug 10, 2023
7 checks passed
@lukasjuhrich lukasjuhrich deleted the improve_handle_errors branch September 6, 2023 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant