Skip to content

Commit

Permalink
extract error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed May 20, 2020
1 parent 97953a5 commit 01ebd96
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions qcodes/instrument/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1902,31 +1902,35 @@ def get(self, get_if_invalid: bool = True) -> ParamDataType:
if gettable:
return self._parameter.get()
else:
if self._timestamp is None:
error_msg = (f"Value of parameter "
f"{(self._parameter.full_name)} "
f"is unknown and the Parameter "
f"does not have a get command. "
f"Please set the value before "
f"attempting to get it.")
elif self._max_val_age is not None:
# TODO: this check should really be at the time
# of setting max_val_age unfortunately this
# happens in init before get wrapping is performed.
error_msg = ("`max_val_age` is not supported "
"for a parameter without get "
"command.")
else:
# max_val_age is None and TS is not None but cache is
# invalid with the current logic that should never
# happen
error_msg = ("Cannot return cache of a parameter "
"that does not have a get command "
"and has an invalid cache")
error_msg = self._construct_error_msg()
raise RuntimeError(error_msg)
else:
return self._value

def _construct_error_msg(self) -> str:
if self._timestamp is None:
error_msg = (f"Value of parameter "
f"{self._parameter.full_name} "
f"is unknown and the Parameter "
f"does not have a get command. "
f"Please set the value before "
f"attempting to get it.")
elif self._max_val_age is not None:
# TODO: this check should really be at the time
# of setting max_val_age unfortunately this
# happens in init before get wrapping is performed.
error_msg = ("`max_val_age` is not supported "
"for a parameter without get "
"command.")
else:
# max_val_age is None and TS is not None but cache is
# invalid with the current logic that should never
# happen
error_msg = ("Cannot return cache of a parameter "
"that does not have a get command "
"and has an invalid cache")
return error_msg

def __call__(self) -> ParamDataType:
"""
Same as :meth:`get` but always call ``get`` on parameter if the
Expand Down

0 comments on commit 01ebd96

Please sign in to comment.