Skip to content

Commit c031ca3

Browse files
committed
Be more resilient to IO errors when compressing tracebacks (fix #31)
1 parent b6212de commit c031ca3

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

backslash/error_container.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
from ._compat import TextIOWrapper
66

7+
import logbook
78
from sentinels import NOTHING
89

910

11+
_logger = logbook.Logger(__name__)
12+
13+
1014
class ErrorContainer(object):
1115

1216
def add_error(self, message, exception_type=NOTHING, traceback=NOTHING, timestamp=NOTHING, is_failure=NOTHING, exception_attrs=NOTHING):
@@ -39,17 +43,25 @@ def add_error(self, message, exception_type=NOTHING, traceback=NOTHING, timestam
3943
returned = self.client.api.call_function('add_error', kwargs) # pylint: disable=no-member
4044

4145
if has_streaming_upload and traceback_info is not NOTHING:
42-
traceback_url = returned.api_url.add_path('traceback')
43-
with tempfile.TemporaryFile(mode='w+b') as traceback_file:
46+
self._compress_traceback(returned, traceback_info)
47+
48+
return returned
49+
50+
def _compress_traceback(self, error, traceback_info):
51+
traceback_url = error.api_url.add_path('traceback')
52+
with tempfile.TemporaryFile(mode='w+b') as traceback_file:
53+
try:
4454
with gzip.GzipFile(fileobj=traceback_file, mode='w+b') as compressed_file_raw:
4555
with TextIOWrapper(compressed_file_raw) as compressed_file:
4656
json.dump(traceback_info, compressed_file)
57+
except IOError:
58+
_logger.error('Unable to compress traceback on disk. Reporting error without traceback', exc_info=True)
59+
return
4760

48-
traceback_file.seek(0)
49-
resp = self.client.api.session.put(traceback_url, data=traceback_file) # pylint: disable=no-member
50-
resp.raise_for_status()
51-
returned.refresh()
52-
return returned
61+
traceback_file.seek(0)
62+
resp = self.client.api.session.put(traceback_url, data=traceback_file) # pylint: disable=no-member
63+
resp.raise_for_status()
64+
error.refresh()
5365

5466
def add_failure(self, message, **kwargs):
5567
return self.add_error(message, is_failure=True, **kwargs)

0 commit comments

Comments
 (0)