Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logging in DbtLocalBaseOperator #1004

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cosmos/operators/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,8 @@ def handle_exception_subprocess(self, result: FullOutputSubprocessResult) -> Non
if self.skip_exit_code is not None and result.exit_code == self.skip_exit_code:
raise AirflowSkipException(f"dbt command returned exit code {self.skip_exit_code}. Skipping.")
elif result.exit_code != 0:
raise AirflowException(
f"dbt command failed. The command returned a non-zero exit code {result.exit_code}. Details: ",
*result.full_output,
)
logger.error("\n".join(result.full_output))
raise AirflowException(f"dbt command failed. The command returned a non-zero exit code {result.exit_code}.")

def handle_exception_dbt_runner(self, result: dbtRunnerResult) -> None:
"""dbtRunnerResult has an attribute `success` that is False if the command failed."""
Expand Down
20 changes: 20 additions & 0 deletions tests/operators/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
parse_number_of_warnings_dbt_runner,
parse_number_of_warnings_subprocess,
)
from cosmos.hooks.subprocess import FullOutputSubprocessResult
from cosmos.operators.local import (
DbtBuildLocalOperator,
DbtDocsAzureStorageLocalOperator,
Expand Down Expand Up @@ -914,3 +915,22 @@ def test_dbt_local_operator_on_kill_sigterm(mock_send_sigterm) -> None:
dbt_base_operator.on_kill()

mock_send_sigterm.assert_called_once()


def test_handle_exception_subprocess(caplog):
"""
Test the handle_exception_subprocess method of the DbtLocalBaseOperator class for non-zero dbt exit code.
"""
operator = ConcreteDbtLocalBaseOperator(
profile_config=None,
task_id="my-task",
project_dir="my/dir",
)
result = FullOutputSubprocessResult(exit_code=1, output="test", full_output=["n" * n for n in range(1, 1000)])

caplog.set_level(logging.ERROR)
# Test when exit_code is non-zero
with pytest.raises(AirflowException) as err_context:
operator.handle_exception_subprocess(result)
assert len(str(err_context.value)) < 100 # Ensure the error message is not too long
assert len(caplog.text) > 1000 # Ensure the log message is not truncated
Loading