question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raising exceptions without logging them

See original GitHub issue

Whenever exceptions are raised they’re logged in the console (and in Sentry if it’s used).

Many of these exceptions are only intended to be shown to the user. For example, django-graphql-jwt raises the PermissionDenied exception for the login_required decorator.

The problem is this pollutes the console output during testing/development and logs unnecessary errors to Sentry. For exceptions such as the example above, that’s only intended to be shown to the user.

As a workaround I’ve tried writing middleware that catches any exceptions thrown:

class ExceptionFilterMiddleware:
    IGNORED_EXCEPTIONS = (
        # Local exceptions
        ValidationException,
        # Third-party exceptions
        JSONWebTokenExpired,
        PermissionDenied,
    )

    def on_error(self, error):
        if not isinstance(error, self.IGNORED_EXCEPTIONS):
            return error

    def resolve(self, next, *args, **kwargs):
        return next(*args, **kwargs).catch(self.on_error)

But if an exception is caught, it no longer populates the errors field in query/mutation output. Therefore all errors are logged, there’s no way to conditionally log exceptions.

This means the only solution is to create a logging filter like the following:

def skip_valid_exceptions(record):
    """
    Skip exceptions for errors only intended to be displayed to the API user.
    """
    skip: bool = False

    if record.exc_info:
        exc_type, exc_value = record.exc_info[:2]
        skip = isinstance(exc_value, valid_exceptions)

    return not skip

But this doesn’t work either because record.exc_info is None whenever an error is thrown with Graphene, therefore it’s not possible to conditionally filter out exceptions based on their type.

Is there a solution for this? Seems like it’d be a common issue but I’ve had trouble finding one. Something like this would be nice to be able to implement. Thanks in advance.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:14

github_iconTop GitHub Comments

7reactions
dspacejscommented, Jan 3, 2020

Responding to remove the bot’s label. I still have this issue and it’s important.

5reactions
stale[bot]commented, Dec 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Logging and raising an exception - Stack Overflow
It depends on the situation, but logging and then raising an exception is generally considered an antipattern. It's redundant and clutters logs.
Read more >
Exceptional Logging of Exceptions in Python - Loggly
Aaron Maxwell shares 6 Python exception logging patterns that show why ... Here, you are catching an exception, logging it, then raising a ......
Read more >
Either log or rethrow Java exceptions, but never do both
When an exception occurs in your Java code, you can log it or you can rethrow it -- but don't do both. Here's...
Read more >
Exceptions in Python: catching, raising, customizing, and ...
If you generically catch all exceptions regardless of what caused them, logging and monitoring systems may not be able to capture these errors, ......
Read more >
Handling exceptions in Python like a PRO - Gui Commits
You don't have to log the exception object. The exception function of the logger is intended to be used as-is inside except blocks....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found