Skip to content

Commit

Permalink
change exception names
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Dec 14, 2023
1 parent 0e4f7d9 commit 2bc13de
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 140 deletions.
7 changes: 0 additions & 7 deletions docs/source/api/etl_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ Update methods
.. automodule:: gene.etl.update
:members:

Exceptions
----------

.. automodule:: gene.etl.exceptions
:members:
:show-inheritance:

NCBI
----

Expand Down
6 changes: 0 additions & 6 deletions docs/source/api/query_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ Query API
:members:
:special-members: __init__
:undoc-members:

.. autoexception:: gene.query.InvalidParameterException
:members:
:undoc-members:
:show-inheritance:

6 changes: 3 additions & 3 deletions src/gene/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import click

from gene.database import create_db
from gene.database.database import DatabaseException
from gene.database.database import DatabaseError
from gene.etl.update import update_all_sources, update_normalized, update_source
from gene.schemas import SourceName

Expand Down Expand Up @@ -142,7 +142,7 @@ def update_from_remote(data_url: Optional[str], db_url: str, silent: bool) -> No
f"Error: Fetching remote data dump not supported for {db.__class__.__name__}"
) # noqa: E501
click.get_current_context().exit(1)
except DatabaseException as e:
except DatabaseError as e:
click.echo(f"Encountered exception during update: {str(e)}")
click.get_current_context().exit(1)

Expand Down Expand Up @@ -225,7 +225,7 @@ def dump_database(output_directory: Path, db_url: str, silent: bool) -> None:
f"Error: Dumping data to file not supported for {db.__class__.__name__}"
) # noqa: E501
click.get_current_context().exit(1)
except DatabaseException as e:
except DatabaseError as e:
click.echo(f"Encountered exception during update: {str(e)}")
click.get_current_context().exit(1)

Expand Down
8 changes: 4 additions & 4 deletions src/gene/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from .database import (
AWS_ENV_VAR_NAME,
AbstractDatabase,
DatabaseException,
DatabaseInitializationException,
DatabaseReadException,
DatabaseWriteException,
DatabaseError,
DatabaseInitializationError,
DatabaseReadError,
DatabaseWriteError,
create_db,
)
32 changes: 16 additions & 16 deletions src/gene/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
from gene.schemas import RecordType, RefType, SourceMeta, SourceName


class DatabaseException(Exception): # noqa: N818
class DatabaseError(Exception):
"""Create custom class for handling database exceptions"""


class DatabaseInitializationException(DatabaseException):
class DatabaseInitializationError(DatabaseError):
"""Create custom exception for errors during DB connection initialization."""


class DatabaseReadException(DatabaseException):
class DatabaseReadError(DatabaseError):
"""Create custom exception for lookup/read errors"""


class DatabaseWriteException(DatabaseException):
class DatabaseWriteError(DatabaseError):
"""Create custom exception for write errors"""


Expand All @@ -43,7 +43,7 @@ def __init__(self, db_url: Optional[str] = None, **db_args) -> None:
:param db_url: address/connection description for database
:param db_args: any DB implementation-specific parameters
:raise DatabaseInitializationException: if initial setup fails
:raise DatabaseInitializationError: if initial setup fails
"""

@abc.abstractmethod
Expand All @@ -58,12 +58,12 @@ def _check_delete_okay() -> bool:
"""Check that environmental conditions permit DB deletion, and require
confirmation.
:raise DatabaseWriteException: if skip confirmation variable is set -- manual
:raise DatabaseWriteError: if skip confirmation variable is set -- manual
approval is required.
"""
if environ.get(AWS_ENV_VAR_NAME, "") == AwsEnvName.PRODUCTION:
if environ.get(SKIP_AWS_DB_ENV_NAME, "") == "true":
raise DatabaseWriteException(
raise DatabaseWriteError(
f"Must unset {SKIP_AWS_DB_ENV_NAME} env variable to enable drop_db()" # noqa: E501
)
return click.confirm("Are you sure you want to delete existing data?")
Expand All @@ -75,8 +75,8 @@ def drop_db(self) -> None:
"""Initiate total teardown of DB. Useful for quickly resetting the entirety of
the data. Requires manual confirmation.
:raise DatabaseWriteException: if called in a protected setting with
confirmation silenced.
:raise DatabaseWriteError: if called in a protected setting with confirmation
silenced.
"""

@abc.abstractmethod
Expand All @@ -103,7 +103,7 @@ def initialize_db(self) -> None:
existing content -- ie, this method is also responsible for checking whether
the DB is already set up.
:raise DatabaseInitializationException: if initialization fails
:raise DatabaseInitializationError: if initialization fails
"""

@abc.abstractmethod
Expand Down Expand Up @@ -168,7 +168,7 @@ def add_source_metadata(self, src_name: SourceName, data: SourceMeta) -> None:
:param src_name: name of source
:param data: known source attributes
:raise DatabaseWriteException: if write fails
:raise DatabaseWriteError: if write fails
"""

@abc.abstractmethod
Expand All @@ -192,27 +192,27 @@ def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: AN
:param concept_id: record to update
:param merge_ref: new ref value
:raise DatabaseWriteException: if attempting to update non-existent record
:raise DatabaseWriteError: if attempting to update non-existent record
"""

@abc.abstractmethod
def delete_normalized_concepts(self) -> None:
"""Remove merged records from the database. Use when performing a new update
of normalized data.
:raise DatabaseReadException: if DB client requires separate read calls and
:raise DatabaseReadError: if DB client requires separate read calls and
encounters a failure in the process
:raise DatabaseWriteException: if deletion call fails
:raise DatabaseWriteError: if deletion call fails
"""

@abc.abstractmethod
def delete_source(self, src_name: SourceName) -> None:
"""Delete all data for a source. Use when updating source data.
:param src_name: name of source to delete
:raise DatabaseReadException: if DB client requires separate read calls and
:raise DatabaseReadError: if DB client requires separate read calls and
encounters a failure in the process
:raise DatabaseWriteException: if deletion call fails
:raise DatabaseWriteError: if deletion call fails
"""

@abc.abstractmethod
Expand Down
42 changes: 21 additions & 21 deletions src/gene/database/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
VALID_AWS_ENV_NAMES,
AbstractDatabase,
AwsEnvName,
DatabaseInitializationException,
DatabaseReadException,
DatabaseWriteException,
DatabaseInitializationError,
DatabaseReadError,
DatabaseWriteError,
confirm_aws_db_use,
)
from gene.schemas import (
Expand All @@ -44,20 +44,20 @@ def __init__(self, db_url: Optional[str] = None, **db_args) -> None:
:Keyword Arguments:
* region_name: AWS region (defaults to "us-east-2")
* silent: if True, suppress console output
:raise DatabaseInitializationException: if initial setup fails
:raise DatabaseInitializationError: if initial setup fails
"""
self.gene_table = environ.get("GENE_DYNAMO_TABLE", "gene_normalizer")
region_name = db_args.get("region_name", "us-east-2")

if AWS_ENV_VAR_NAME in environ:
if "GENE_TEST" in environ:
raise DatabaseInitializationException(
raise DatabaseInitializationError(
f"Cannot have both GENE_TEST and {AWS_ENV_VAR_NAME} set."
) # noqa: E501
try:
aws_env = AwsEnvName(environ[AWS_ENV_VAR_NAME])
except ValueError:
raise DatabaseInitializationException(
raise DatabaseInitializationError(
f"{AWS_ENV_VAR_NAME} must be one of {VALID_AWS_ENV_NAMES}: found {environ[AWS_ENV_VAR_NAME]} instead."
)
skip_confirmation = environ.get(SKIP_AWS_DB_ENV_NAME)
Expand Down Expand Up @@ -108,13 +108,13 @@ def list_tables(self) -> List[str]:
def drop_db(self) -> None:
"""Delete all tables from database. Requires manual confirmation.
:raise DatabaseWriteException: if called in a protected setting with
confirmation silenced.
:raise DatabaseWriteError: if called in a protected setting with confirmation
silenced.
"""
try:
if not self._check_delete_okay():
return
except DatabaseWriteException as e:
except DatabaseWriteError as e:
raise e

if self.gene_table in self.list_tables():
Expand Down Expand Up @@ -226,7 +226,7 @@ def get_source_metadata(self, src_name: Union[str, SourceName]) -> Dict:
Key={"label_and_type": pk, "concept_id": concept_id}
).get("Item")
if not metadata:
raise DatabaseReadException(
raise DatabaseReadError(
f"Unable to retrieve data for source {src_name}"
)
self._cached_sources[src_name] = metadata
Expand Down Expand Up @@ -362,7 +362,7 @@ def add_source_metadata(self, src_name: SourceName, metadata: SourceMeta) -> Non
:param src_name: name of source
:param data: known source attributes
:raise DatabaseWriteException: if write fails
:raise DatabaseWriteError: if write fails
"""
src_name_value = src_name.value
metadata_item = metadata.model_dump()
Expand All @@ -373,7 +373,7 @@ def add_source_metadata(self, src_name: SourceName, metadata: SourceMeta) -> Non
try:
self.genes.put_item(Item=metadata_item)
except ClientError as e:
raise DatabaseWriteException(e)
raise DatabaseWriteError(e)

def add_record(self, record: Dict, src_name: SourceName) -> None:
"""Add new record to database.
Expand Down Expand Up @@ -458,7 +458,7 @@ def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: AN
:param concept_id: record to update
:param merge_ref: new ref value
:raise DatabaseWriteException: if attempting to update non-existent record
:raise DatabaseWriteError: if attempting to update non-existent record
"""
label_and_type = f"{concept_id.lower()}##identity"
key = {"label_and_type": label_and_type, "concept_id": concept_id}
Expand All @@ -475,7 +475,7 @@ def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: AN
except ClientError as e:
code = e.response.get("Error", {}).get("Code")
if code == "ConditionalCheckFailedException":
raise DatabaseWriteException(
raise DatabaseWriteError(
f"No such record exists for keys {label_and_type}, {concept_id}"
)
else:
Expand All @@ -488,9 +488,9 @@ def delete_normalized_concepts(self) -> None:
"""Remove merged records from the database. Use when performing a new update
of normalized data.
:raise DatabaseReadException: if DB client requires separate read calls and
:raise DatabaseReadError: if DB client requires separate read calls and
encounters a failure in the process
:raise DatabaseWriteException: if deletion call fails
:raise DatabaseWriteError: if deletion call fails
"""
while True:
with self.genes.batch_writer(
Expand All @@ -504,7 +504,7 @@ def delete_normalized_concepts(self) -> None:
),
)
except ClientError as e:
raise DatabaseReadException(e)
raise DatabaseReadError(e)
records = response["Items"]
if not records:
break
Expand All @@ -520,9 +520,9 @@ def delete_source(self, src_name: SourceName) -> None:
"""Delete all data for a source. Use when updating source data.
:param src_name: name of source to delete
:raise DatabaseReadException: if DB client requires separate read calls and
:raise DatabaseReadError: if DB client requires separate read calls and
encounters a failure in the process
:raise DatabaseWriteException: if deletion call fails
:raise DatabaseWriteError: if deletion call fails
"""
while True:
try:
Expand All @@ -531,7 +531,7 @@ def delete_source(self, src_name: SourceName) -> None:
KeyConditionExpression=Key("src_name").eq(src_name.value),
)
except ClientError as e:
raise DatabaseReadException(e)
raise DatabaseReadError(e)
records = response["Items"]
if not records:
break
Expand All @@ -547,7 +547,7 @@ def delete_source(self, src_name: SourceName) -> None:
}
)
except ClientError as e:
raise DatabaseWriteException(e)
raise DatabaseWriteError(e)

def complete_write_transaction(self) -> None:
"""Conclude transaction or batch writing if relevant."""
Expand Down
Loading

0 comments on commit 2bc13de

Please sign in to comment.