Skip to content

Commit

Permalink
Changed the saving of datasets to include exception handling (#443)
Browse files Browse the repository at this point in the history
Co-authored-by: Jorgen-5 <[email protected]>
  • Loading branch information
Jorgen-5 and Jorgen-5 authored Nov 19, 2024
1 parent f7a1425 commit 45566ac
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/datadoc/frontend/callbacks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,34 @@ def save_metadata_and_generate_alerts(metadata: Datadoc) -> list:

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
metadata.write_metadata_document()
success_alert = build_ssb_alert(
AlertTypes.SUCCESS,
"Lagret metadata",
)

for warning in w:
if issubclass(warning.category, ObligatoryDatasetWarning):
missing_obligatory_dataset = str(warning.message)
elif issubclass(warning.category, ObligatoryVariableWarning):
missing_obligatory_variables = str(warning.message)
else:
logger.warning(
"An unexpected warning was caught: %s",
warning.message,
try:
metadata.write_metadata_document()
success_alert = build_ssb_alert(
AlertTypes.SUCCESS,
"Lagret metadata",
)
for warning in w:
if issubclass(warning.category, ObligatoryDatasetWarning):
missing_obligatory_dataset = str(warning.message)
elif issubclass(warning.category, ObligatoryVariableWarning):
missing_obligatory_variables = str(warning.message)
else:
logger.warning(
"An unexpected warning was caught: %s",
warning.message,
)
except (ValueError, FileNotFoundError) as e:
if isinstance(e, ValueError):
logger.exception("Unable to save metadata document, no dataset found")
elif isinstance(e, FileNotFoundError):
logger.exception(
"Unable to save metadata document, file %s not found",
metadata.dataset_path,
)
success_alert = build_ssb_alert(
AlertTypes.ERROR,
"Kunne ikke lagre metadata",
)

return [
success_alert,
Expand Down

0 comments on commit 45566ac

Please sign in to comment.