Skip to content

Commit

Permalink
Merge pull request #382 from 3fonov/main
Browse files Browse the repository at this point in the history
Remove stacktrace from database errors
  • Loading branch information
BentsiLeviav authored Nov 13, 2024
2 parents 065f3a7 + ba346f3 commit bd45c3c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Unreleased
### Improvement
* Added support for [range_hashed](https://clickhouse.com/docs/en/sql-reference/dictionaries#range_hashed) and [complex_key_range_hashed](https://clickhouse.com/docs/en/sql-reference/dictionaries#complex_key_range_hashed) layouts to the dictionary materialization. ([#361](https://github.com/ClickHouse/dbt-clickhouse/pull/361))
* Truncated stack trace for database errors for cleaner output when HIDE_STACK_TRACE variable is set to any value.

### Release [1.8.4], 2024-09-17
### Improvement
Expand Down
10 changes: 7 additions & 3 deletions dbt/adapters/clickhouse/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
from dbt.adapters.clickhouse import ClickHouseColumn
from dbt.adapters.clickhouse.__version__ import version as dbt_clickhouse_version
from dbt.adapters.clickhouse.dbclient import ChClientWrapper, ChRetryableException
from dbt.adapters.clickhouse.util import hide_stack_trace


class ChHttpClient(ChClientWrapper):
def query(self, sql, **kwargs):
try:
return self._client.query(sql, **kwargs)
except DatabaseError as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def command(self, sql, **kwargs):
try:
return self._client.command(sql, **kwargs)
except DatabaseError as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def columns_in_query(self, sql: str, **kwargs) -> List[ClickHouseColumn]:
try:
Expand All @@ -34,7 +37,8 @@ def columns_in_query(self, sql: str, **kwargs) -> List[ClickHouseColumn]:
for name, ch_type in zip(query_result.column_names, query_result.column_types)
]
except DatabaseError as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def get_ch_setting(self, setting_name):
setting = self._client.server_settings.get(setting_name)
Expand Down
10 changes: 7 additions & 3 deletions dbt/adapters/clickhouse/nativeclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dbt.adapters.clickhouse.__version__ import version as dbt_clickhouse_version
from dbt.adapters.clickhouse.dbclient import ChClientWrapper, ChRetryableException
from dbt.adapters.clickhouse.logger import logger
from dbt.adapters.clickhouse.util import hide_stack_trace

try:
driver_version = pkg_resources.get_distribution('clickhouse-driver').version
Expand All @@ -22,15 +23,17 @@ def query(self, sql, **kwargs):
try:
return NativeClientResult(self._client.execute(sql, with_column_types=True, **kwargs))
except clickhouse_driver.errors.Error as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def command(self, sql, **kwargs):
try:
result = self._client.execute(sql, **kwargs)
if len(result) and len(result[0]):
return result[0][0]
except clickhouse_driver.errors.Error as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def columns_in_query(self, sql: str, **kwargs) -> List[ClickHouseColumn]:
try:
Expand All @@ -40,7 +43,8 @@ def columns_in_query(self, sql: str, **kwargs) -> List[ClickHouseColumn]:
)
return [ClickHouseColumn.create(column[0], column[1]) for column in columns]
except clickhouse_driver.errors.Error as ex:
raise DbtDatabaseError(str(ex).strip()) from ex
err_msg = hide_stack_trace(ex)
raise DbtDatabaseError(err_msg) from ex

def get_ch_setting(self, setting_name):
try:
Expand Down
11 changes: 10 additions & 1 deletion dbt/adapters/clickhouse/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
import os

from dbt_common.exceptions import DbtRuntimeError

Expand All @@ -13,3 +13,12 @@ def compare_versions(v1: str, v2: str) -> int:
except ValueError:
raise DbtRuntimeError("Version must consist of only numbers separated by '.'")
return 0


def hide_stack_trace(ex: Exception) -> str:

if not os.getenv("HIDE_STACK_TRACE", ''):
return str(ex).strip()

err_msg = str(ex).split("Stack trace")[0].strip()
return err_msg
1 change: 0 additions & 1 deletion tests/integration/adapter/dictionary/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os

import pytest

from dbt.tests.util import run_dbt

testing_s3 = os.environ.get('DBT_CH_TEST_INCLUDE_S3', '').lower() in ('1', 'true', 'yes')
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from dbt.adapters.clickhouse.util import compare_versions
from unittest.mock import patch

from dbt.adapters.clickhouse.util import compare_versions, hide_stack_trace


def test_is_before_version():
Expand All @@ -11,3 +13,19 @@ def test_is_before_version():
assert compare_versions('22.0.0', '21.0.0') == 1
assert compare_versions('21.0.1', '21.0.0') == 1
assert compare_versions('21.0.1', '21.0') == 0


def test_hide_stack_trace_no_env_var():
# Test when HIDE_STACK_TRACE is not set
with patch('os.getenv', return_value=''):
exception = Exception("Error occurred\nStack trace details follow...")
result = hide_stack_trace(exception)
assert result == "Error occurred\nStack trace details follow..."


def test_hide_stack_trace_env_var_set():
# Test when HIDE_STACK_TRACE is set
with patch('os.getenv', return_value='1'):
exception = Exception("Error occurred\nStack trace details follow...")
result = hide_stack_trace(exception)
assert result == "Error occurred"

0 comments on commit bd45c3c

Please sign in to comment.