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 log_errors decorator for background and async functions #376

Merged
merged 2 commits into from
Jan 11, 2025

Conversation

whusterj
Copy link
Member

@whusterj whusterj commented Jan 9, 2025

What this does

I've been meaning to add this for a long while. This is a common pattern I use on many projects to wrap things like background tasks that operate outside of Django's default exception handler.

This catches and explicitly logs any exception in the wrapped function, both to the default logger and to Rollbar if it is enabled.

Usage:

@log_errors
def my_background_task(...):
    # Do something in the background outside of Django's and
    # DRF's request-response context and default exception handler.
    # Any exceptions that happen will be caught and logged as usual

@whusterj whusterj self-assigned this Jan 9, 2025
@whusterj whusterj requested a review from paribaker January 9, 2025 19:58
@oudeismetis oudeismetis temporarily deployed to tn-spa-bootstrapper-pr-376 January 9, 2025 20:00 Inactive
@whusterj whusterj temporarily deployed to tn-spa-bootstrapper-pr-376 January 9, 2025 20:03 Inactive
@whusterj whusterj changed the title Add a log_errors decorator Add a log_errors decorator for async functions Jan 9, 2025
@whusterj whusterj changed the title Add a log_errors decorator for async functions Add a log_errors decorator for background and async functions Jan 9, 2025
Copy link
Contributor

@oudeismetis oudeismetis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it! ❤️

@whusterj whusterj merged commit 7df9e61 into main Jan 11, 2025
18 checks passed
@whusterj whusterj deleted the log-errors-decorator branch January 11, 2025 19:29
@OkayItsMikael
Copy link
Contributor

👏🏾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants