Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions raven/contrib/tornado/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AsyncSentryClient(Client):
"""
def __init__(self, *args, **kwargs):
self.validate_cert = kwargs.pop('validate_cert', True)
self.ignored_exception_types = kwargs.pop('ignored_exception_types', [])
super(AsyncSentryClient, self).__init__(*args, **kwargs)

def capture(self, *args, **kwargs):
Expand Down Expand Up @@ -225,7 +226,11 @@ def log_exception(self, typ, value, tb):
log_exception() is added in Tornado v3.1.
"""
rv = super(SentryMixin, self).log_exception(typ, value, tb)
self.captureException(exc_info=(typ, value, tb))

client = self.get_sentry_client()
if not any(typ is ignored_type for ignored_type in client.ignored_exception_types):
self.captureException(exc_info=(typ, value, tb))

return rv

def send_error(self, status_code=500, **kwargs):
Expand All @@ -236,6 +241,9 @@ def send_error(self, status_code=500, **kwargs):
return super(SentryMixin, self).send_error(status_code, **kwargs)
else:
rv = super(SentryMixin, self).send_error(status_code, **kwargs)
if 500 <= status_code <= 599:
self.captureException(exc_info=kwargs.get('exc_info'))
exc_info = kwargs.get('exc_info')
if 500 <= status_code <= 599 and exc_info:
client = self.get_sentry_client()
if not any(exc_info[0] is ignored_type for ignored_type in client.ignored_exception_types):
self.captureException(exc_info=exc_info)
return rv
26 changes: 26 additions & 0 deletions tests/contrib/tornado/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def get(self):
raise Exception("Oops")


class SendIgnoredCustomErrorTestHandler(SentryMixin, web.RequestHandler):
class CustomError(Exception):
pass

def get(self):
raise self.CustomError("A Custom Error!")


class SendErrorAsyncHandler(SentryMixin, web.RequestHandler):
@web.asynchronous
@gen.engine
Expand Down Expand Up @@ -212,3 +220,21 @@ def test_send_error_handler_async(self, send):

user_data = kwargs['user']
self.assertEqual(user_data['is_authenticated'], False)


class TornadoAsyncClientWithIgnoredExceptionsTestCase(testing.AsyncHTTPTestCase):
def get_app(self):
app = web.Application([
web.url(r'/an-ignored-error', SendIgnoredCustomErrorTestHandler),
])
app.sentry_client = AsyncSentryClient(
'http://public_key:secret_key@host:9000/project',
ignored_exception_types=[SendIgnoredCustomErrorTestHandler.CustomError],
)
return app

@patch('raven.contrib.tornado.AsyncSentryClient.send')
def test_send_an_ignored_error(self, send):
response = self.fetch('/an-ignored-error?qs=qs')
self.assertEqual(response.code, 500)
self.assertEqual(send.call_count, 0)