diff options
author | David Cramer <dcramer@gmail.com> | 2016-07-25 15:49:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-25 15:49:28 -0700 |
commit | 6e75e1d5e87c4bc1a2b3486b8f53a43d4431f07c (patch) | |
tree | f5a3757de92bf88215881057c5953505823093ea | |
parent | 2369418a50de098f074cbe5ccffab0227eb77b2d (diff) | |
parent | 667fb9daaafecea8e03879334bd68f8583544a26 (diff) | |
download | raven-6e75e1d5e87c4bc1a2b3486b8f53a43d4431f07c.tar.gz |
Merge pull request #793 from OpenGov/captureZipStackError
Added IOError to get stack info for graceful zip import errors
-rw-r--r-- | raven/utils/stacks.py | 2 | ||||
-rw-r--r-- | tests/utils/stacks/tests.py | 33 |
2 files changed, 32 insertions, 3 deletions
diff --git a/raven/utils/stacks.py b/raven/utils/stacks.py index 6c17cad..7933313 100644 --- a/raven/utils/stacks.py +++ b/raven/utils/stacks.py @@ -30,7 +30,7 @@ def get_lines_from_file(filename, lineno, context_lines, if loader is not None and hasattr(loader, "get_source"): try: source = loader.get_source(module_name) - except ImportError: + except (ImportError, IOError): # Traceback (most recent call last): # File "/Users/dcramer/Development/django-sentry/sentry/client/handlers.py", line 31, in emit # get_client().create_from_record(record, request=request) diff --git a/tests/utils/stacks/tests.py b/tests/utils/stacks/tests.py index 4d1bc36..a0cc5be 100644 --- a/tests/utils/stacks/tests.py +++ b/tests/utils/stacks/tests.py @@ -2,10 +2,10 @@ from __future__ import unicode_literals import six +import os.path from mock import Mock from raven.utils.testutils import TestCase - from raven.utils.stacks import get_culprit, get_stack_info, get_lines_from_file @@ -95,11 +95,40 @@ class GetStackInfoTest(TestCase): assert results['frames'][8]['filename'] == '8' assert results['frames'][9]['filename'] == '9' +class FailLoader(): + ''' + Recreating the built-in loaders from a fake stack trace was brittle. + This method ensures its testing the path where the loader is defined + but fails with known exceptions. + ''' + def get_source(self, module_name): + if '.py' in module_name: + raise ImportError('Cannot load .py files') + elif '.zip' in module_name: + raise IOError('Cannot load .zip files') + else: + raise ValueError('Invalid file extension') class GetLineFromFileTest(TestCase): + def setUp(self): + self.loader = FailLoader() + def test_non_ascii_file(self): - import os.path filename = os.path.join(os.path.dirname(__file__), 'utf8_file.txt') self.assertEqual( get_lines_from_file(filename, 3, 1), (['Some code here'], '', ['lorem ipsum'])) + + def test_missing_zip_get_source(self): + filename = 'does_not_exist.zip' + module = 'not.zip.loadable' + self.assertEqual( + get_lines_from_file(filename, 3, 1, self.loader, module), + (None, None, None)) + + def test_missing_get_source(self): + filename = 'does_not_exist.py' + module = 'not.py.loadable' + self.assertEqual( + get_lines_from_file(filename, 3, 1, self.loader, module), + (None, None, None)) |