Skip to content

Commit 65f6727

Browse files
fix(dagster-dbt): improve error messages for missing dbt adapter and invalid target_path
- Wrap `dbt.adapters` import in `compat.py` with an actionable ModuleNotFoundError that suggests installing the correct adapter package - Add the same ModuleNotFoundError guard in `_initialize_dbt_core_adapter` for the `dbt.adapters.factory` import - Split the bare `except:` in `cli()` adapter init into a specific ModuleNotFoundError branch with an installation hint, and an Exception fallback for other errors - Add an explicit isinstance check for `target_path` in `cli()` to surface a clear ValueError instead of an AttributeError when a str is passed
1 parent 2804c08 commit 65f6727

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

python_modules/libraries/dagster-dbt/dagster_dbt/compat.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@
4040
REFABLE_NODE_TYPES: list[str] = []
4141
else:
4242
if DBT_PYTHON_VERSION is not None:
43-
from dbt.adapters.base.impl import (
44-
BaseAdapter as BaseAdapter,
45-
BaseColumn as BaseColumn,
46-
BaseRelation as BaseRelation,
47-
)
43+
try:
44+
from dbt.adapters.base.impl import (
45+
BaseAdapter as BaseAdapter,
46+
BaseColumn as BaseColumn,
47+
BaseRelation as BaseRelation,
48+
)
49+
except ModuleNotFoundError as e:
50+
raise ModuleNotFoundError(
51+
"A dbt adapter package could not be found.\n\n"
52+
"Please install the appropriate dbt adapter for your data platform "
53+
"(for example: dbt-postgres, dbt-bigquery, or dbt-snowflake).\n\n"
54+
f"Original error:\n{e}"
55+
) from e
4856
from dbt.contracts.results import NodeStatus, TestStatus
4957
from dbt.node_types import NodeType as NodeType
5058

python_modules/libraries/dagster-dbt/dagster_dbt/core/resource.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,15 @@ def _get_unique_target_path(
361361
return current_target_path.joinpath(path)
362362

363363
def _initialize_dbt_core_adapter(self, args: Sequence[str]) -> BaseAdapter:
364-
from dbt.adapters.factory import get_adapter, register_adapter, reset_adapters
364+
try:
365+
from dbt.adapters.factory import get_adapter, register_adapter, reset_adapters
366+
except ModuleNotFoundError as e:
367+
raise ModuleNotFoundError(
368+
"A dbt adapter package could not be found.\n\n"
369+
"Please install the appropriate dbt adapter for your data platform "
370+
"(for example: dbt-postgres, dbt-bigquery, or dbt-snowflake).\n\n"
371+
f"Original error:\n{e}"
372+
) from e
365373
from dbt.config import RuntimeConfig
366374
from dbt.config.runtime import load_profile, load_project
367375
from dbt.config.utils import parse_cli_vars
@@ -646,7 +654,13 @@ def my_dbt_op(dbt: DbtCliResource):
646654
dagster_dbt_translator = updated_params.dagster_dbt_translator
647655
selection_args = updated_params.selection_args
648656
indirect_selection = updated_params.indirect_selection
649-
target_path = target_path or self._get_unique_target_path(context=context)
657+
raw_target_path = target_path or self._get_unique_target_path(context=context)
658+
if not isinstance(raw_target_path, Path):
659+
raise ValueError(
660+
f"The 'target_path' argument must be a pathlib.Path, not {type(raw_target_path).__name__!r}."
661+
" Pass a Path object, for example: target_path=Path('target')."
662+
)
663+
target_path = raw_target_path
650664
project_dir = Path(
651665
updated_params.dbt_project.project_dir
652666
if updated_params.dbt_project
@@ -717,7 +731,22 @@ def my_dbt_op(dbt: DbtCliResource):
717731
if self._cli_version.major < 2:
718732
try:
719733
adapter = self._initialize_dbt_core_adapter(args)
720-
except:
734+
except ModuleNotFoundError as e:
735+
if "dbt.adapters" in str(e) or "dbt-" in str(e):
736+
logger.warning(
737+
"A dbt adapter package could not be loaded. Please install the"
738+
" appropriate dbt adapter for your data platform (for example:"
739+
" dbt-postgres, dbt-bigquery, or dbt-snowflake). Some Dagster"
740+
" features that require a live connection to your data platform"
741+
" will be unavailable.\n\nOriginal error: %s",
742+
e,
743+
)
744+
else:
745+
logger.warning(
746+
"An error was encountered when creating a handle to the dbt adapter in Dagster.",
747+
exc_info=True,
748+
)
749+
except Exception:
721750
logger.warning(
722751
"An error was encountered when creating a handle to the dbt adapter in Dagster.",
723752
exc_info=True,

0 commit comments

Comments
 (0)