diff --git a/buildingmotif/database/table_connection.py b/buildingmotif/database/table_connection.py index b1ce2f7a9..e18659fa5 100644 --- a/buildingmotif/database/table_connection.py +++ b/buildingmotif/database/table_connection.py @@ -13,6 +13,7 @@ DBTemplate, DepsAssociation, ) +from buildingmotif.database.errors import LibraryNotFound, TemplateNotFound class TableConnection: @@ -216,7 +217,10 @@ def get_db_library(self, id: int) -> DBLibrary: :return: DBLibrary :rtype: DBLibrary """ - db_library = self.bm.session.query(DBLibrary).filter(DBLibrary.id == id).one() + try: + db_library = self.bm.session.query(DBLibrary).filter(DBLibrary.id == id).one() + except NoResultFound: + raise LibraryNotFound(f"Library with id {id} not found") return db_library def get_db_library_by_name(self, name: str) -> DBLibrary: @@ -298,9 +302,12 @@ def get_db_template(self, id: int) -> DBTemplate: :return: DBTemplate :rtype: DBTemplate """ - db_template = ( - self.bm.session.query(DBTemplate).filter(DBTemplate.id == id).one() - ) + try: + db_template = ( + self.bm.session.query(DBTemplate).filter(DBTemplate.id == id).one() + ) + except NoResultFound: + raise TemplateNotFound(idnum=id) return db_template def get_db_template_by_name(self, name: str) -> DBTemplate: @@ -316,7 +323,7 @@ def get_db_template_by_name(self, name: str) -> DBTemplate: self.bm.session.query(DBTemplate).filter(DBTemplate.name == name).one() ) except NoResultFound: - raise NoResultFound(f"No template found with name {name}") + raise TemplateNotFound(name=name) return db_template def get_library_defining_db_template(self, id: int) -> DBLibrary: diff --git a/buildingmotif/dataclasses/library.py b/buildingmotif/dataclasses/library.py index d0bf70ead..d12afa07b 100644 --- a/buildingmotif/dataclasses/library.py +++ b/buildingmotif/dataclasses/library.py @@ -313,7 +313,6 @@ def _infer_templates_from_graph(self, graph: rdflib.Graph): logging.warning( f"An ontology could not resolve a dependency on {dependency} ({e}). Check this is loaded into BuildingMOTIF" ) - get_building_motif().session.rollback() continue class_candidates = set(graph.subjects(rdflib.RDF.type, rdflib.OWL.Class)) shape_candidates = set(graph.subjects(rdflib.RDF.type, rdflib.SH.NodeShape))