Skip to content

Commit

Permalink
Improve error logging in DbtLocalBaseOperator (#1004)
Browse files Browse the repository at this point in the history
Improve error logging when the `dbt` command returns a non-zero exit
code. Instead of raising an `AirflowException` with the full output, log
the output using the logger and then raise the exception with a concise
error message. This makes the dbt output more readable and not in a
single line as AirflowException logs message in a single line, and it can 
get very long. 

closes #1003 
---------

Co-authored-by: David Steinar Asgrimsson <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 31, 2024
1 parent 712ef7d commit 6ceb7f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
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

0 comments on commit 6ceb7f2

Please sign in to comment.