Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 729a7b9

Browse files
authored
Merge pull request #419 from dlawin/issue_417_error_handling
Issue 417 error handling
2 parents d2d7849 + 896ed2c commit 729a7b9

File tree

3 files changed

+171
-135
lines changed

3 files changed

+171
-135
lines changed

data_diff/dbt.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import rich
66
from dataclasses import dataclass
77
from packaging.version import parse as parse_version
8-
from typing import List, Optional, Dict
8+
from typing import List, Optional, Dict, Tuple
99

1010
import requests
1111

@@ -28,7 +28,7 @@ def import_dbt():
2828
send_event_json,
2929
is_tracking_enabled,
3030
)
31-
from .utils import run_as_daemon, truncate_error
31+
from .utils import get_from_dict_with_raise, run_as_daemon, truncate_error
3232
from . import connect_to_table, diff_tables, Algorithm
3333

3434
RUN_RESULTS_PATH = "/target/run_results.json"
@@ -308,18 +308,40 @@ def set_project_dict(self):
308308
with open(self.project_dir + PROJECT_FILE) as project:
309309
self.project_dict = self.yaml.safe_load(project)
310310

311-
def set_connection(self):
312-
with open(self.profiles_dir + PROFILES_FILE) as profiles:
311+
def _get_connection_creds(self) -> Tuple[Dict[str, str], str]:
312+
profiles_path = self.profiles_dir + PROFILES_FILE
313+
with open(profiles_path) as profiles:
313314
profiles = self.yaml.safe_load(profiles)
314315

315316
dbt_profile = self.project_dict.get("profile")
316-
profile_outputs = profiles.get(dbt_profile)
317-
profile_target = profile_outputs.get("target")
318-
credentials = profile_outputs.get("outputs").get(profile_target)
319-
conn_type = credentials.get("type").lower()
317+
318+
profile_outputs = get_from_dict_with_raise(
319+
profiles, dbt_profile, f"No profile '{dbt_profile}' found in '{profiles_path}'."
320+
)
321+
profile_target = get_from_dict_with_raise(
322+
profile_outputs, "target", f"No target found in profile '{dbt_profile}' in '{profiles_path}'."
323+
)
324+
outputs = get_from_dict_with_raise(
325+
profile_outputs, "outputs", f"No outputs found in profile '{dbt_profile}' in '{profiles_path}'."
326+
)
327+
credentials = get_from_dict_with_raise(
328+
outputs,
329+
profile_target,
330+
f"No credentials found for target '{profile_target}' in profile '{dbt_profile}' in '{profiles_path}'.",
331+
)
332+
conn_type = get_from_dict_with_raise(
333+
credentials,
334+
"type",
335+
f"No type found for target '{profile_target}' in profile '{dbt_profile}' in '{profiles_path}'.",
336+
)
337+
conn_type = conn_type.lower()
320338

321339
# values can contain env_vars
322340
rendered_credentials = self.ProfileRenderer().render_data(credentials)
341+
return rendered_credentials, conn_type
342+
343+
def set_connection(self):
344+
rendered_credentials, conn_type = self._get_connection_creds()
323345

324346
if conn_type == "snowflake":
325347
if rendered_credentials.get("password") is None or rendered_credentials.get("private_key_path") is not None:

data_diff/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import re
3-
from typing import Iterable, Sequence
3+
from typing import Dict, Iterable, Sequence
44
from urllib.parse import urlparse
55
import operator
66
import threading
@@ -79,6 +79,13 @@ def truncate_error(error: str):
7979
return re.sub("'(.*?)'", "'***'", first_line)
8080

8181

82+
def get_from_dict_with_raise(dictionary: Dict, key: str, error_message: str):
83+
result = dictionary.get(key)
84+
if result is None:
85+
raise ValueError(error_message)
86+
return result
87+
88+
8289
class Vector(tuple):
8390

8491
"""Immutable implementation of a regular vector over any arithmetic value

0 commit comments

Comments
 (0)