Skip to content

Commit 21b7515

Browse files
authored
Bundle Upload - Error made clear when there is a problem in yaml (#2309)
* safeguards added in bundle unpacking and made unkown error clear for users * string formatting fixed
1 parent 1ad888d commit 21b7515

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/apps/competitions/tasks.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,14 @@ def mark_status_as_failed_and_delete_dataset(competition_creation_status, detail
353353
with NamedTemporaryFile(mode="w+b") as temp_file:
354354
logger.info(f"Download competition bundle: {competition_dataset.data_file.name}")
355355
competition_bundle_url = make_url_sassy(competition_dataset.data_file.url)
356-
with requests.get(competition_bundle_url, stream=True) as r:
357-
r.raise_for_status()
358-
for chunk in r.iter_content(chunk_size=8192):
359-
temp_file.write(chunk)
360-
r.close()
356+
try:
357+
with requests.get(competition_bundle_url, stream=True) as r:
358+
r.raise_for_status()
359+
for chunk in r.iter_content(chunk_size=8192):
360+
temp_file.write(chunk)
361+
r.close()
362+
except requests.exceptions.RequestException as e:
363+
raise CompetitionUnpackingException(f"Failed to download bundle from storage: {e}")
361364

362365
# seek back to the start of the tempfile after writing to it..
363366
temp_file.seek(0)
@@ -371,10 +374,17 @@ def mark_status_as_failed_and_delete_dataset(competition_creation_status, detail
371374
# Read metadata (competition.yaml)
372375
yaml_path = os.path.join(temp_directory, "competition.yaml")
373376
if not os.path.exists(yaml_path):
374-
raise CompetitionUnpackingException("competition.yaml is missing from zip, check your folder structure "
375-
"to make sure it is in the root directory.")
376-
with open(yaml_path) as f:
377-
competition_yaml = yaml.safe_load(f.read())
377+
raise CompetitionUnpackingException(
378+
"competition.yaml is missing from zip, check your folder structure "
379+
"to make sure it is in the root directory."
380+
)
381+
try:
382+
with open(yaml_path) as f:
383+
competition_yaml = yaml.safe_load(f.read())
384+
except yaml.YAMLError as e:
385+
raise CompetitionUnpackingException(f"Error parsing competition.yaml: {e}")
386+
except Exception as e:
387+
raise CompetitionUnpackingException(f"Failed to read competition.yaml: {e}")
378388

379389
yaml_version = str(competition_yaml.get('version', '1'))
380390

@@ -428,11 +438,14 @@ def _get_error_string(error_dict):
428438
mark_status_as_failed_and_delete_dataset(status, message)
429439
raise e
430440

431-
except Exception as e: # noqa: E722
441+
except Exception as e:
432442
# These are critical uncaught exceptions, make sure the end user is at least informed
433443
# that unpacking has failed -- do not share unhandled exception details
434444
logger.error(traceback.format_exc())
435-
message = "Unpacking the bundle failed. Here is the error log: {}".format(e)
445+
if isinstance(e, KeyError):
446+
message = f"Unpacking the bundle failed. A required key or referenced index ({e}) was not found in the YAML. Check that all mandatory fields are present and that any item referenced by index is correctly defined."
447+
else:
448+
message = f"Unpacking the bundle failed. Here is the error log: {e}"
436449
mark_status_as_failed_and_delete_dataset(status, message)
437450

438451

0 commit comments

Comments
 (0)