diff --git a/tangostationcontrol/tangostationcontrol/toolkit/retriever.py b/tangostationcontrol/tangostationcontrol/toolkit/retriever.py index 6506ca3c79939ee9fea6c3ab0240938234d83cd4..827164e22a7543277d0138443d772154606e6370 100644 --- a/tangostationcontrol/tangostationcontrol/toolkit/retriever.py +++ b/tangostationcontrol/tangostationcontrol/toolkit/retriever.py @@ -74,10 +74,8 @@ class Retriever(ABC): result = self.session.query(self.ab.Attribute.att_conf_id).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] - except TypeError as e: - raise Exception(f"Attribute {attribute_fqname} not found!") from e - except NoResultFound as e: - raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e + except (TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e @abstractmethod def get_attribute_datatype(self,attribute_fqname: str): @@ -103,8 +101,8 @@ class Retriever(ABC): join(self.ab.Attribute,self.ab.Attribute.att_conf_id==base_class.att_conf_id).\ filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= time_delta_db, \ base_class.data_time <= time_now_db)).order_by(base_class.data_time).all() - except AttributeError as e: - raise Exception(f"Empty result: Attribute {attribute_fqname} not found") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e return result def get_attribute_value_by_interval(self,attribute_fqname: str, start_time: datetime, stop_time: datetime, tablename:str): @@ -121,8 +119,8 @@ class Retriever(ABC): join(self.ab.Attribute,self.ab.Attribute.att_conf_id==base_class.att_conf_id).\ filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= str(start_time), \ base_class.data_time <= str(stop_time))).order_by(base_class.data_time).all() - except AttributeError as e: - raise Exception(f"Empty result: Attribute {attribute_fqname} not found") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e return result class RetrieverMySQL(Retriever): @@ -162,10 +160,8 @@ class RetrieverMySQL(Retriever): result = self.session.query(self.ab.DataType.data_type).join(self.ab.Attribute,self.ab.Attribute.att_conf_data_type_id==self.ab.DataType.att_conf_data_type_id).\ filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] - except TypeError as e: - raise Exception(f"Attribute not {attribute_fqname} found!") from e - except NoResultFound as e: - raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e def get_attribute_value_by_hours(self,attribute_fqname: str, hours: float = 1.0): """ @@ -258,10 +254,8 @@ class RetrieverTimescale(Retriever): result = self.session.query(self.ab.DataType.type).join(self.ab.Attribute,self.ab.Attribute.att_conf_type_id==self.ab.DataType.att_conf_type_id).\ filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] - except TypeError as e: - raise Exception(f"Attribute not {attribute_fqname} found!") from e - except NoResultFound as e: - raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e def get_attribute_format(self,attribute_fqname: str): """ @@ -274,10 +268,8 @@ class RetrieverTimescale(Retriever): result = self.session.query(self.ab.Format.format).join(self.ab.Attribute,self.ab.Attribute.att_conf_format_id==self.ab.Format.att_conf_format_id).\ filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] - except TypeError as e: - raise Exception("Attribute not found!") from e - except NoResultFound as e: - raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e def get_attribute_tablename(self,attribute_fqname: str): """ @@ -289,10 +281,8 @@ class RetrieverTimescale(Retriever): result = self.session.query(self.ab.Attribute.table_name).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] - except TypeError as e: - raise Exception("Attribute not found!") from e - except NoResultFound as e: - raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e + except (AttributeError, TypeError, NoResultFound) as e: + raise ValueError(f"Attribute {attribute_fqname} not found!") from e def get_attribute_value_by_hours(self, attribute_fqname: str, hours: float = 1.0): """ @@ -311,4 +301,4 @@ class RetrieverTimescale(Retriever): """ tablename = self.get_attribute_tablename(attribute_fqname) return super().get_attribute_value_by_interval(attribute_fqname,start_time,stop_time,tablename) - \ No newline at end of file +