Search code examples
pythondjangosentry

How to ignore some errors with Sentry (not to send them)?


I have a project based on django (3.2.10) and sentry-sdk (1.16.0) There is my sentry-init file:

from os import getenv


SENTRY_URL = getenv('SENTRY_URL')

if SENTRY_URL:
    from sentry_sdk import init
    from sentry_sdk.integrations.django import DjangoIntegration
    from sentry_sdk.integrations.redis import RedisIntegration
    from sentry_sdk.integrations.celery import CeleryIntegration

    init(
        dsn=SENTRY_URL,
        integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
        traces_sample_rate=1.0,
        send_default_pii=True,
        debug=True,
    )

I have a CustomError inherited from Exception

Every time I raise the CustomError sentry-sdk sends it to the dsn-url.

I want to ignore some class of error or something like this. How can I do this?


Solution

  • You can pass a function that filters the errors to be sent:

    from os import getenv
    
    
    SENTRY_URL = getenv('SENTRY_URL')
    
    if SENTRY_URL:
        from sentry_sdk import init
        from sentry_sdk.integrations.django import DjangoIntegration
        from sentry_sdk.integrations.redis import RedisIntegration
        from sentry_sdk.integrations.celery import CeleryIntegration
      
        def before_send(event, hint):
           if 'exc_info' in hint:
              exc_type, exc_value, tb = hint['exc_info']
              if isinstance(exc_value, CustomError):  # Replace CustomError with your custom error 
                 return None
           return event
    
        init(
            dsn=SENTRY_URL,
            integrations=[DjangoIntegration(), RedisIntegration(), CeleryIntegration()],
            traces_sample_rate=1.0,
            send_default_pii=True,
            debug=True,
            before_send=before_send
        )
    

    You can find more info in the documentation.