diff options
author | Xavier Ordoquy <xordoquy@linovia.com> | 2013-10-10 04:52:54 -0700 |
---|---|---|
committer | Xavier Ordoquy <xordoquy@linovia.com> | 2013-10-10 04:52:54 -0700 |
commit | cb4804ccdc3f12c9416de8b030fd42d1b785bd46 (patch) | |
tree | a9ba6453decf59f55230e4d01c176558a701c7a3 | |
parent | 33e19809a4df81631ed42b24d82aa821d6ea91f2 (diff) | |
parent | 997371b6e0b27a3976448db4b567d560c0d24560 (diff) | |
download | raven-cb4804ccdc3f12c9416de8b030fd42d1b785bd46.tar.gz |
Merge pull request #371 from razzmatazz/raven-flask-ignore-exception-config
Raven flask ignore exception config
-rw-r--r-- | docs/config/flask.rst | 9 | ||||
-rw-r--r-- | raven/contrib/flask/__init__.py | 7 | ||||
-rw-r--r-- | tests/contrib/flask/tests.py | 32 |
3 files changed, 47 insertions, 1 deletions
diff --git a/docs/config/flask.rst b/docs/config/flask.rst index fd92b0e..d32a1a6 100644 --- a/docs/config/flask.rst +++ b/docs/config/flask.rst @@ -45,6 +45,15 @@ By default, only the ``id`` (current_user.get_id()), ``is_authenticated``, and ` ``email`` will be captured as ``sentry.interfaces.User.email``, and any additionl attributes will be available under ``sentry.interfaces.User.data`` +You can specify the types of exceptions that should not be reported by Sentry client in your application by setting the ``RAVEN_IGNORE_EXCEPTIONS`` configuration value on your Flask app configuration:: + + class MyExceptionType(Exception): + def __init__(self, message): + super(MyExceptionType, self).__init__(message) + + app = Flask(__name__) + app.config["RAVEN_IGNORE_EXCEPTIONS"] = [MyExceptionType] + Usage ----- diff --git a/raven/contrib/flask/__init__.py b/raven/contrib/flask/__init__.py index 00c43bb..3ef9c85 100644 --- a/raven/contrib/flask/__init__.py +++ b/raven/contrib/flask/__init__.py @@ -8,6 +8,7 @@ raven.contrib.flask from __future__ import absolute_import +import sys import os from flask import request @@ -80,6 +81,12 @@ class Sentry(object): if not self.client: return + ignored_exc_type_list = self.app.config.get('RAVEN_IGNORE_EXCEPTIONS', []) + exc = sys.exc_info()[1] + + if any((isinstance(exc, ignored_exc_type) for ignored_exc_type in ignored_exc_type_list)): + return + self.client.captureException( exc_info=kwargs.get('exc_info'), data=get_data_from_request(request), diff --git a/tests/contrib/flask/tests.py b/tests/contrib/flask/tests.py index 16aa0d1..3453e8c 100644 --- a/tests/contrib/flask/tests.py +++ b/tests/contrib/flask/tests.py @@ -29,12 +29,15 @@ class User(AnonymousUserMixin): get_id = lambda x: 1 -def create_app(): +def create_app(ignore_exceptions=None): import os app = Flask(__name__) app.config['SECRET_KEY'] = os.urandom(40) + if ignore_exceptions: + app.config['RAVEN_IGNORE_EXCEPTIONS'] = ignore_exceptions + @app.route('/an-error/', methods=['GET', 'POST']) def an_error(): raise ValueError('hello world') @@ -84,6 +87,12 @@ class BaseTest(TestCase): self.raven = TempStoreClient() self.middleware = Sentry(self.app, client=self.raven) + def make_client_and_raven(self, *args, **kwargs): + app = create_app(*args, **kwargs) + raven = TempStoreClient() + Sentry(app, client=raven) + return app.test_client(), raven + class FlaskTest(BaseTest): def test_does_add_to_extensions(self): @@ -192,6 +201,27 @@ class FlaskTest(BaseTest): http = event['sentry.interfaces.Http'] self.assertEqual({}, http.get('data')) + def test_error_handler_with_ignored_exception(self): + client, raven = self.make_client_and_raven(ignore_exceptions=[NameError, ValueError]) + + response = client.get('/an-error/') + self.assertEquals(response.status_code, 500) + self.assertEquals(len(raven.events), 0) + + def test_error_handler_with_exception_not_ignored(self): + client, raven = self.make_client_and_raven(ignore_exceptions=[NameError, KeyError]) + + response = client.get('/an-error/') + self.assertEquals(response.status_code, 500) + self.assertEquals(len(raven.events), 1) + + def test_error_handler_with_empty_ignore_exceptions_list(self): + client, raven = self.make_client_and_raven(ignore_exceptions=[]) + + response = client.get('/an-error/') + self.assertEquals(response.status_code, 500) + self.assertEquals(len(raven.events), 1) + class FlaskLoginTest(BaseTest): @before |