|
4 | 4 |
|
5 | 5 | from ._compat import TextIOWrapper |
6 | 6 |
|
| 7 | +import logbook |
7 | 8 | from sentinels import NOTHING |
8 | 9 |
|
9 | 10 |
|
| 11 | +_logger = logbook.Logger(__name__) |
| 12 | + |
| 13 | + |
10 | 14 | class ErrorContainer(object): |
11 | 15 |
|
12 | 16 | 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 |
39 | 43 | returned = self.client.api.call_function('add_error', kwargs) # pylint: disable=no-member |
40 | 44 |
|
41 | 45 | 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: |
44 | 54 | with gzip.GzipFile(fileobj=traceback_file, mode='w+b') as compressed_file_raw: |
45 | 55 | with TextIOWrapper(compressed_file_raw) as compressed_file: |
46 | 56 | 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 |
47 | 60 |
|
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() |
53 | 65 |
|
54 | 66 | def add_failure(self, message, **kwargs): |
55 | 67 | return self.add_error(message, is_failure=True, **kwargs) |
|
0 commit comments