Skip to content

Commit

Permalink
feat: improve excpetion handling
Browse files Browse the repository at this point in the history
  • Loading branch information
felixscherz committed Jan 16, 2025
1 parent d1c825c commit 59a78cb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
35 changes: 35 additions & 0 deletions moto/s3tables/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,50 @@ def __init__(self) -> None:
super().__init__(self.msg)


class InvalidMetadataLocation(BadRequestException):
msg = "The specified metadata location is not valid."

def __init__(self) -> None:
super().__init__(self.msg)


class NotFoundException(JsonRESTError):
code = 404

def __init__(self, message: str) -> None:
super().__init__("NotFoundException", message)


class NamespaceDoesNotExist(NotFoundException):
msg = "The specified namespace does not exist."

def __init__(self) -> None:
super().__init__(self.msg)


class TableDoesNotExist(NotFoundException):
msg = "The specified table does not exist."

def __init__(self) -> None:
super().__init__(self.msg)


class ConflictException(JsonRESTError):
code = 409

def __init__(self, message: str) -> None:
super().__init__("ConflictException", message)


class VersionTokenMismatch(ConflictException):
msg = "Provided version token does not match the table version token."

def __init__(self) -> None:
super().__init__(self.msg)


class TableAlreadyExists(ConflictException):
msg = "A table with an identical name already exists in the namespace."

def __init__(self) -> None:
super().__init__(self.msg)
52 changes: 30 additions & 22 deletions moto/s3tables/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
InvalidTableBucketName,
InvalidTableName,
NotFoundException,
TableAlreadyExists,
TableDoesNotExist,
VersionTokenMismatch,
)
from moto.utilities.utils import get_partition

Expand Down Expand Up @@ -117,7 +120,7 @@ def update_metadata_location(
self, metadata_location: str, version_token: str
) -> None:
if not self.version_token == version_token:
raise ValueError("version tokens don't match")
raise VersionTokenMismatch()

self.metadata_location = metadata_location
self.version_token = self._generate_version_token()
Expand Down Expand Up @@ -307,31 +310,35 @@ def create_table(
format: Literal["ICEBERG"],
) -> Table:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
ns = bucket.namespaces[namespace]
if name in ns.tables:
raise ValueError("Table already exists")
table = Table(
name=name,
account_id=self.account_id,
created_by=self.account_id,
format=format,
namespace=namespace,
table_bucket_arn=table_bucket_arn,
type="customer",
managed_by_service=False,
partition=self.partition,
)
ns.tables[table.name] = table
return table
raise ValueError("resource doesnt exist")
if not bucket:
raise NotFoundException("The specified bucket does not exist.")

if namespace not in bucket.namespaces:
raise NotFoundException("The specified namespace does not exist.")

ns = bucket.namespaces[namespace]
if name in ns.tables:
TableAlreadyExists()
table = Table(
name=name,
account_id=self.account_id,
created_by=self.account_id,
format=format,
namespace=namespace,
table_bucket_arn=table_bucket_arn,
type="customer",
managed_by_service=False,
partition=self.partition,
)
ns.tables[table.name] = table
return table

def get_table(self, table_bucket_arn: str, namespace: str, name: str) -> Table:
bucket = self.table_buckets.get(table_bucket_arn)
if bucket and namespace in bucket.namespaces:
if name in bucket.namespaces[namespace].tables:
return bucket.namespaces[namespace].tables[name]
raise ValueError("table does not exist")
raise TableDoesNotExist()

def list_tables(
self,
Expand Down Expand Up @@ -402,7 +409,7 @@ def delete_table(
if name in ns.tables:
ns.tables.pop(name)
return
raise ValueError("Table doesn't exist")
raise TableDoesNotExist()

def update_table_metadata_location(
self,
Expand All @@ -420,7 +427,8 @@ def update_table_metadata_location(
metadata_location=metadata_location, version_token=version_token
)
return table
raise ValueError("table does not exist")

raise TableDoesNotExist()


s3tables_backends = BackendDict(
Expand Down

0 comments on commit 59a78cb

Please sign in to comment.