Skip to content

Commit

Permalink
Added new exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Feb 2, 2024
1 parent be8df69 commit 82e3c97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
14 changes: 14 additions & 0 deletions pepdbagent/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def __init__(self, msg=""):
super().__init__(f"""Sample does not exist. {msg}""")


class SampleAlreadyExistsError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""Sample already exists. {msg}""")


class ViewNotFoundError(PEPDatabaseAgentError):
def __init__(self, msg=""):
super().__init__(f"""View does not exist. {msg}""")
Expand All @@ -73,3 +78,12 @@ class SampleAlreadyInView(PEPDatabaseAgentError):

def __init__(self, msg=""):
super().__init__(f"""Sample is already in the view. {msg}""")


class ViewAlreadyExistsError(PEPDatabaseAgentError):
"""
View is already in the project exception
"""

def __init__(self, msg=""):
super().__init__(f"""View already in the project. {msg}""")
4 changes: 2 additions & 2 deletions pepdbagent/modules/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
DEFAULT_TAG,
PKG_NAME,
)
from pepdbagent.exceptions import SampleNotFoundError
from pepdbagent.exceptions import SampleNotFoundError, SampleAlreadyExistsError

from pepdbagent.db_utils import BaseEngine, Samples, Projects

Expand Down Expand Up @@ -241,7 +241,7 @@ def add(
)

if sample_mapping and not overwrite:
raise ValueError(
raise SampleAlreadyExistsError(
f"Sample {namespace}/{name}:{tag}?{sample_name} already exists in the database"
)
elif sample_mapping and overwrite:
Expand Down
20 changes: 16 additions & 4 deletions pepdbagent/modules/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SampleAlreadyInView,
ProjectNotFoundError,
SampleNotFoundError,
ViewAlreadyExistsError,
)

from pepdbagent.db_utils import BaseEngine, Samples, Projects, Views, ViewSampleAssociation
Expand Down Expand Up @@ -188,7 +189,13 @@ def create(
raise SampleNotFoundError(
f"Sample {view_dict.project_namespace}/{view_dict.project_name}:{view_dict.project_tag}:{sample_name} does not exist"
)
sa_session.add(ViewSampleAssociation(sample_id=sample_id, view=view))
try:
sa_session.add(ViewSampleAssociation(sample_id=sample_id, view=view))

except IntegrityError:
raise ViewAlreadyExistsError(
f"View {view_name} of the project {view_dict.project_namespace}/{view_dict.project_name}:{view_dict.project_tag} already exists"
)

sa_session.commit()

Expand Down Expand Up @@ -334,10 +341,15 @@ def remove_sample(
ViewSampleAssociation.view_id == view.id,
)
)
sa_session.execute(delete_statement)
sa_session.commit()
try:
sa_session.execute(delete_statement)
sa_session.commit()
except IntegrityError:
raise SampleNotFoundError(
f"Sample {namespace}/{name}:{tag}:{sample_name} does not exist in view {view_name}"
)

return None
return None

def get_snap_view(
self, namespace: str, name: str, tag: str, sample_name_list: List[str], raw: bool = False
Expand Down

0 comments on commit 82e3c97

Please sign in to comment.