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

Add a context manager for calling log.failure when code fails #12188

Closed
glyph opened this issue May 30, 2024 · 0 comments · Fixed by #12207
Closed

Add a context manager for calling log.failure when code fails #12188

glyph opened this issue May 30, 2024 · 0 comments · Fixed by #12207

Comments

@glyph
Copy link
Member

glyph commented May 30, 2024

Right now we have a pattern of doing this:

log = Logger()

try:
    applicationCode(parameter)
except BaseException:
    log.failure("while doing xyz with {parameter}", parameter=parameter)
else:
    proceed()

This is verbose, error-prone, and kinda reads backwards. Instead, something like this would be nicer:

log = Logger()
with log.whenThereIsAnyProblemLogThis("while doing xyz with {parameter}", parameter=parameter) as operation:
    applicationCode(parameter)
if operation.succeeded:
    proceed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant