From 45990233c973974ce3b0fe52ea41074555a8aa65 Mon Sep 17 00:00:00 2001 From: Li Yazhou Date: Tue, 11 Aug 2015 17:28:56 +0800 Subject: [PATCH 1/4] record ignored_exc_types in tornado AsyncSentryClient --- raven/contrib/tornado/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/raven/contrib/tornado/__init__.py b/raven/contrib/tornado/__init__.py index 4b1645ab9..6868d12fa 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_exc_types = kwargs.pop('ignored_exc_types', []) super(AsyncSentryClient, self).__init__(*args, **kwargs) def capture(self, *args, **kwargs): From cd29067b5048a834a1e52ec1dfaf044276fa6c91 Mon Sep 17 00:00:00 2001 From: Li Yazhou Date: Tue, 11 Aug 2015 17:33:02 +0800 Subject: [PATCH 2/4] add filter on client.ignored_exc_types --- raven/contrib/tornado/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/raven/contrib/tornado/__init__.py b/raven/contrib/tornado/__init__.py index 6868d12fa..2a16f0548 100644 --- a/raven/contrib/tornado/__init__.py +++ b/raven/contrib/tornado/__init__.py @@ -19,7 +19,7 @@ class AsyncSentryClient(Client): """ def __init__(self, *args, **kwargs): self.validate_cert = kwargs.pop('validate_cert', True) - self.ignored_exc_types = kwargs.pop('ignored_exc_types', []) + self.ignored_exception_types = kwargs.pop('ignored_exception_types', []) super(AsyncSentryClient, self).__init__(*args, **kwargs) def capture(self, *args, **kwargs): @@ -226,6 +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) + + client = self.get_sentry_client() + if any(typ is ignored_type for ignored_type in client.ignored_exception_types): + return + self.captureException(exc_info=(typ, value, tb)) return rv From 4b5d3d33b7c1f9e16df8fd2b0f44c8371201be7d Mon Sep 17 00:00:00 2001 From: Li Yazhou Date: Tue, 11 Aug 2015 17:54:44 +0800 Subject: [PATCH 3/4] add test case for ignored exceptions --- tests/contrib/tornado/tests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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) From dab776dc197db743f58f25d931b7a6f055699281 Mon Sep 17 00:00:00 2001 From: Li Yazhou Date: Mon, 17 Aug 2015 15:06:03 +0800 Subject: [PATCH 4/4] filter ignored exceptions in older tornado --- raven/contrib/tornado/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/raven/contrib/tornado/__init__.py b/raven/contrib/tornado/__init__.py index 2a16f0548..1b4700a49 100644 --- a/raven/contrib/tornado/__init__.py +++ b/raven/contrib/tornado/__init__.py @@ -228,10 +228,9 @@ def log_exception(self, typ, value, tb): rv = super(SentryMixin, self).log_exception(typ, value, tb) client = self.get_sentry_client() - if any(typ is ignored_type for ignored_type in client.ignored_exception_types): - return + if not any(typ is ignored_type for ignored_type in client.ignored_exception_types): + self.captureException(exc_info=(typ, value, tb)) - self.captureException(exc_info=(typ, value, tb)) return rv def send_error(self, status_code=500, **kwargs): @@ -242,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