Skip to content

Commit 6c66490

Browse files
committed
Add optional throw exceptions
1 parent 2abdd21 commit 6c66490

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

simvue/sender.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def upload_cached_file(
5656
obj_type: str,
5757
file_path: pydantic.FilePath,
5858
id_mapping: dict[str, str],
59+
throw_exceptions: bool,
5960
retry_failed_uploads: bool,
6061
lock: threading.Lock,
6162
) -> None:
@@ -71,6 +72,10 @@ def upload_cached_file(
7172
The path to the cached file to upload
7273
id_mapping : dict[str, str]
7374
A mapping of offline to online object IDs
75+
throw_exceptions : bool
76+
Whether to throw exceptions, or just log them
77+
retry_failed_uploads : bool
78+
Whether to retry failed uploads or ignore them
7479
lock : threading.Lock
7580
A lock to prevent multiple threads accessing the id mapping directory at once
7681
"""
@@ -83,7 +88,10 @@ def upload_cached_file(
8388

8489
try:
8590
_instance_class = getattr(simvue.api.objects, _exact_type)
86-
except AttributeError:
91+
except AttributeError as error:
92+
if throw_exceptions:
93+
raise error
94+
8795
_logger.error(f"Attempt to initialise unknown type '{_exact_type}'")
8896
_log_upload_failed(file_path)
8997
return
@@ -109,11 +117,13 @@ def upload_cached_file(
109117
_new_id = obj_for_upload.id
110118

111119
except Exception as error:
112-
if "status 409" in error.args[0]:
120+
if "status 409" in str(error):
113121
return
122+
if throw_exceptions:
123+
raise error
114124

115125
_logger.error(
116-
f"Error while committing '{_instance_class.__name__}': {error.args[0]}"
126+
f"Error while committing '{_instance_class.__name__}': {str(error)}"
117127
)
118128
_log_upload_failed(file_path)
119129
return
@@ -182,6 +192,7 @@ def sender(
182192
max_workers: int = 5,
183193
threading_threshold: int = 10,
184194
objects_to_upload: list[str] = UPLOAD_ORDER,
195+
throw_exceptions: bool = False,
185196
retry_failed_uploads: bool = False,
186197
) -> dict[str, str]:
187198
"""Send data from a local cache directory to the Simvue server.
@@ -196,6 +207,8 @@ def sender(
196207
The number of cached files above which threading will be used
197208
objects_to_upload : list[str]
198209
Types of objects to upload, by default uploads all types of objects present in cache
210+
throw_exceptions : bool, optional
211+
Whether to throw exceptions as they are encountered in the sender, default is False (exceptions will be logged)
199212
retry_failed_uploads : bool, optional
200213
Whether to retry sending objects which previously failed, by default False
201214
@@ -238,6 +251,7 @@ def sender(
238251
obj_type=_obj_type,
239252
file_path=file_path,
240253
id_mapping=_id_mapping,
254+
throw_exceptions=throw_exceptions,
241255
retry_failed_uploads=retry_failed_uploads,
242256
lock=_lock,
243257
)
@@ -251,11 +265,15 @@ def sender(
251265
obj_type=_obj_type,
252266
file_path=file_path,
253267
id_mapping=_id_mapping,
268+
throw_exceptions=throw_exceptions,
254269
retry_failed_uploads=retry_failed_uploads,
255270
lock=_lock,
256271
),
257272
_offline_files,
258273
)
274+
# This will raise any exceptions encountered during sending
275+
for result in _results:
276+
pass
259277

260278
# Send heartbeats
261279
_headers: dict[str, str] = {

tests/unit/test_sender.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
import pathlib
1414
import requests
1515

16+
@pytest.mark.parametrize("throw_exceptions", (True, False))
1617
@pytest.mark.parametrize("retry_failed_uploads", (True, False))
1718
@pytest.mark.parametrize("parallel", (True, False))
1819

1920
@pytest.mark.offline
20-
def test_sender_exception_handling(offline_cache_setup, caplog, retry_failed_uploads, parallel):
21+
def test_sender_exception_handling(offline_cache_setup, caplog, throw_exceptions, retry_failed_uploads, parallel):
2122
# Create something which will produce an error when sent, eg a metric with invalid run ID
2223
for i in range(5):
2324
_metrics = Metrics.new(
@@ -33,6 +34,11 @@ def test_sender_exception_handling(offline_cache_setup, caplog, retry_failed_upl
3334
offline=True
3435
)
3536
_metrics.commit()
37+
38+
if throw_exceptions:
39+
with pytest.raises(ValueError):
40+
sender(throw_exceptions=True, threading_threshold=1 if parallel else 10)
41+
return
3642

3743
with caplog.at_level(logging.ERROR):
3844
sender(threading_threshold=1 if parallel else 10)

0 commit comments

Comments
 (0)