diff --git a/raven/contrib/tornado/__init__.py b/raven/contrib/tornado/__init__.py index 4b1645ab9..1b4700a49 100644 --- a/raven/contrib/tornado/__init__.py +++ b/raven/contrib/tornado/__init__.py @@ -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): @@ -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): @@ -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 diff --git a/tests/contrib/tornado/tests.py b/tests/contrib/tornado/tests.py index 0f665362b..ab49ed110 100644 --- a/tests/contrib/tornado/tests.py +++ b/tests/contrib/tornado/tests.py @@ -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 @@ -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)