Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit b2340ce

Browse files
authored
feat: improve error handling and validation (#34)
1 parent 5118722 commit b2340ce

7 files changed

Lines changed: 1241 additions & 738 deletions

File tree

dataform2looker/database_mappers.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
from google.cloud import bigquery
66

7-
from dataform2looker.exceptions import UnsupportedDatabaseTypeError
7+
from dataform2looker.exceptions import (
8+
InvalidFieldTypeError,
9+
TableNotFoundError,
10+
UnsupportedDatabaseTypeError,
11+
)
812

913

1014
class Column:
@@ -48,6 +52,9 @@ def __init__(
4852
data_type: The original database data type of the column.
4953
time_frames: A list of timeframes for time dimension groups (optional).
5054
55+
Raises:
56+
InvalidFieldTypeError: If an unsupported field type is provided.
57+
5158
Sets the attributes and constructs the `column_dictionary` for Looker integration.
5259
If the column is a time dimension group, it adds `timeframes` and `datatype` to the dictionary.
5360
""" # noqa: E501
@@ -56,10 +63,11 @@ def __init__(
5663
self.field_type = field_type
5764
self.data_type = data_type
5865
self.time_frames = time_frames
59-
assert (
60-
self.field_type in self._DIMENSION_TYPE_MAP
61-
), f"Invalid field type, use one of {self._DIMENSION_TYPE_MAP.keys()},\
62-
got {self.field_type}"
66+
if self.field_type not in self._DIMENSION_TYPE_MAP:
67+
raise InvalidFieldTypeError(
68+
field_type=self.field_type,
69+
allowed_types=list(self._DIMENSION_TYPE_MAP.keys()),
70+
)
6371
self.dimension_type = self._DIMENSION_TYPE_MAP[self.field_type]
6472
self.column_dictionary = {
6573
"name": self.name,
@@ -224,11 +232,19 @@ def __get_columns(self) -> list[Column]:
224232
This method connects to BigQuery, fetches the schema of the table identified by `self.table_id`,
225233
and constructs a list of `Column` objects representing each field in the table.
226234
235+
Raises:
236+
TableNotFoundError: If the table is not found or cannot be retrieved.
237+
227238
Returns:
228239
list[Column]: A list of `Column` objects, each representing a column in the BigQuery table.
229240
""" # noqa: E501
230241
client = bigquery.Client()
231-
table = client.get_table(self.table_id)
242+
try:
243+
table = client.get_table(self.table_id)
244+
except Exception as e:
245+
logging.error(f"Failed to retrieve table '{self.table_id}': {e}")
246+
raise TableNotFoundError(self.table_id) from e
247+
232248
logging.debug(f"Got table schema from table {self.table_id}")
233249
columns = [
234250
Column(

dataform2looker/exceptions.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,35 @@ def __init__(self, db_type: str) -> None:
1616
""" # noqa: E501
1717
self.msg_template = f"'{db_type}' is not a supported database type"
1818
super().__init__(self.msg_template)
19+
20+
21+
class InvalidFieldTypeError(Exception):
22+
"""Exception raised when an invalid field type is encountered."""
23+
24+
def __init__(self, field_type: str, allowed_types: list) -> None:
25+
"""Initializes the `InvalidFieldTypeError` exception.
26+
27+
Args:
28+
field_type (str): The unsupported field type.
29+
allowed_types (list): The list of allowed types.
30+
"""
31+
self.msg_template = (
32+
f"Invalid field type, use one of {allowed_types}, got {field_type}"
33+
)
34+
super().__init__(self.msg_template)
35+
36+
37+
class TableNotFoundError(Exception):
38+
"""Exception raised when a table is not found in the database."""
39+
40+
def __init__(self, table_id: str) -> None:
41+
"""Initializes the `TableNotFoundError` exception.
42+
43+
Args:
44+
table_id (str): The ID of the table that was not found.
45+
"""
46+
self.msg_template = (
47+
f"Table '{table_id}' was not found, check permissions "
48+
f"or if the table exists"
49+
)
50+
super().__init__(self.msg_template)

0 commit comments

Comments
 (0)