Skip to content

Commit

Permalink
Use logging.exception() to include stacktrace
Browse files Browse the repository at this point in the history
When an exception has been caught, favor `exception` over `error`, since
`exception()` automatically preserves the error context.
  • Loading branch information
DavidCain committed Jun 17, 2023
1 parent 4951d9c commit 577f1fb
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ ignore = [
"SIM105", # Use `contextlib.suppress`
"SIM117", # Avoid repeated `with` statements
"TRY200", # Use `raise from` to preserve context (also see B904)
"TRY400", # Use logging.exception, not logging.error
]

# By default, `ruff` will respect `.gitignore`
Expand Down
4 changes: 2 additions & 2 deletions ws/signals/signup_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def revoke_existing_task(sender, instance, raw, using, update_fields, **kwargs):
app.control.revoke(trip.lottery_task_id)
except OperationalError:
# Log the exception, but don't raise exceptions, preventing trip saving
logger.error("Failed to revoke lottery task for trip %s", trip.pk)
logger.exception("Failed to revoke lottery task for trip %s", trip.pk)
else:
instance.lottery_task_id = None

Expand All @@ -135,7 +135,7 @@ def add_lottery_task(sender, instance, created, raw, using, update_fields, **kwa
try:
task_id = tasks.run_lottery.apply_async((trip.pk,), eta=trip.signups_close_at)
except OperationalError:
logger.error("Failed to make lottery task for trip %s", trip.pk)
logger.exception("Failed to make lottery task for trip %s", trip.pk)
else:
trip.lottery_task_id = task_id
# Use update() to avoid repeated signal on post_save
Expand Down
2 changes: 1 addition & 1 deletion ws/views/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def post(self, request, *args, **kwargs):
try:
tasks.update_participant_affiliation.delay(participant.pk)
except OperationalError:
logger.error(
logger.exception(
"Unable to update affiliation to %s for participant %s",
participant.pk,
participant.affiliation,
Expand Down

0 comments on commit 577f1fb

Please sign in to comment.