Skip to content

Commit

Permalink
Gracefully handling ELN-related issues. (#257)
Browse files Browse the repository at this point in the history
Add human-readable error messages in case something isn't right.

Co-authored-by: Kevin Jablonka <32935233+kjappelbaum@users.noreply.github.com>
  • Loading branch information
yakutovicha and kjappelbaum committed Oct 14, 2021
1 parent a6b3a75 commit e8f20e4
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions aiidalab_widgets_base/elns.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,48 @@


def connect_to_eln(eln_instance=None, **kwargs):

# assuming that the connection can only be established to the ELNs
# with the stored configuration.
try:
with open(ELN_CONFIG, "r") as file:
config = json.load(file)
except (FileNotFoundError, json.JSONDecodeError, KeyError):
return None
return (
None,
f"Can't open '{ELN_CONFIG}' (ELN configuration file). Instance: {eln_instance}",
)

# If no ELN instance was specified, trying the default one.
if not eln_instance:
eln_instance = config.pop("default", None)

if eln_instance:
if eln_instance: # The ELN instance could be identified.
if eln_instance in config:
eln_config = config[eln_instance]
eln_type = eln_config.pop("eln_type", None)
else:
eln_type = None
else: # The selected instance is not present in the config.
return None, f"Didn't find configuration for the '{eln_instance}' instance."

# If the ELN type cannot be identified - aborting.
if not eln_type:
return None
eln = get_eln_connector(eln_type)(
eln_instance=eln_instance, **eln_config, **kwargs
)
return None, f"Can't identify the type of {eln_instance} ELN."

# Everything is alright, can populate the ELN connector
# with the required info.
try:
eln = get_eln_connector(eln_type)(
eln_instance=eln_instance, **eln_config, **kwargs
)
except NotImplementedError as err:
return None, str(err)
eln.connect()
return eln
return eln, None

return None
return (
None,
"No ELN instance was provided, the default ELN instance is not configured either. Set a default ELN or select an ELN instance.",
)


class ElnImportWidget(ipw.VBox):
Expand All @@ -52,11 +70,11 @@ def __init__(self, path_to_root="../", **kwargs):
error_message = ipw.HTML()
super().__init__(children=[error_message], **kwargs)

eln = connect_to_eln(**kwargs)
eln, msg = connect_to_eln(**kwargs)

if eln is None:
url = f"{path_to_root}aiidalab-widgets-base/notebooks/eln_configure.ipynb"
error_message.value = f"""Warning! The access to ELN {kwargs['eln_instance']} is not configured. Please follow <a href="{url}" target="_blank">the link</a> to configure it."""
error_message.value = f"""Warning! The access to ELN is not configured. Please follow <a href="{url}" target="_blank">the link</a> to configure it.</br> More details: {msg}"""
return

traitlets.dlink((eln, "node"), (self, "node"))
Expand Down Expand Up @@ -93,13 +111,13 @@ def __init__(self, path_to_root="../", **kwargs):
self._output,
self.message,
]
self.eln = connect_to_eln()
self.eln, msg = connect_to_eln()
if self.eln:
traitlets.dlink((self, "node"), (self.eln, "node"))
else:
self.modify_settings.disabled = True
send_button.disabled = True
self.message.value = f"""Warning! The access to an ELN is not configured. Please follow <a href="{self.path_to_root}/aiidalab-widgets-base/notebooks/eln_configure.ipynb" target="_blank">the link</a> to configure it."""
self.message.value = f"""Warning! The access to ELN is not configured. Please follow <a href="{self.path_to_root}/aiidalab-widgets-base/notebooks/eln_configure.ipynb" target="_blank">the link</a> to configure it.</br> </br> More details: {msg}"""

super().__init__(children=children, **kwargs)

Expand Down Expand Up @@ -261,7 +279,14 @@ def check_connection(self, _=None):

def display_eln_config(self, value=None):
"""Display ELN configuration specific to the selected type of ELN."""
eln_class = get_eln_connector(self.eln_types.value)
try:
eln_class = get_eln_connector(self.eln_types.value)
except NotImplementedError as err:
with self._output:
clear_output()
display(ipw.HTML("&#10060;" + str(err)))
return

self.eln = eln_class(
eln_instance=self.eln_instance.label if self.eln_instance.value else "",
**self.eln_instance.value,
Expand Down

0 comments on commit e8f20e4

Please sign in to comment.