要忽略所有相关错误,有两种方法:
- 使用 before_send
import sentry_sdk
from rest_framework.exceptions import ValidationError
def before_send(event, hint):
if 'exc_info' in hint:
exc_type, exc_value, tb = hint['exc_info']
if isinstance(exc_value, (KeyError, ValidationError)):
return None
return event
sentry_sdk.init(
dsn='SENTRY_DSN',
before_send=before_send
)
- 使用ignore_errors
import sentry_sdk
from rest_framework.exceptions import ValidationError
sentry_dsk.init(
dsn='SENTRY_DSN',
ignore_errors=[
KeyError() ,
ValidationError('my error message'),
] # All event error (KeyError, ValidationError) will be ignored
)
要忽略特定事件错误,只需使用自定义def before_send 忽略此事件ValidationError('my error message'):
def before_send(event, hint):
if 'exc_info' in hint:
exc_type, exc_value, tb = hint['exc_info']
if exc_value.args[0] in ['my error message', 'my error message 2', ...]:
return None
return event
这记录在 sentry-python 文档中:https://sentry-python.readthedocs.io/en/latest/api.html