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

Decode ClickHouse Error Response to UTF-8 #19

Merged
merged 2 commits into from
Jun 17, 2022
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
7 changes: 5 additions & 2 deletions clickhouse_connect/driver/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, interface: str, host: str, port: int, username: str, password
:param query_limit: Default LIMIT on returned rows
:param connect_timeout: Timeout in seconds for the http connection
:param send_receive_timeout: Read timeout in seconds for http connection
:param client_name: Http user agent header value
:param send_progress: Ask ClickHouse to send progress headers. Used for summary and keep alive
:param verify: Verify the server certificate in secure/https mode
:param ca_cert: If verify is True, the file path to Certificate Authority root to validate ClickHouse server
certificate, in .pem format. Ignored if verify is False. This is not necessary if the ClickHouse server
Expand Down Expand Up @@ -234,8 +236,9 @@ def _raw_request(self, data=None, method='POST', params: Optional[Dict] = None,
err_str = f'HTTPDriver url {self.url} returned response code {response.status_code})'
logger.error(err_str)
if response.content:
logger.error(str(response.content))
err_str = f':{err_str}\n {str(response.content)[0:240]}'
err_msg = response.content.decode(errors='backslashreplace')
logger.error(str(err_msg))
err_str = f':{err_str}\n {err_msg[0:240]}'
if attempts > retries or response.status_code not in (429, 503, 504):
raise DatabaseError(err_str)

Expand Down
8 changes: 8 additions & 0 deletions tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from clickhouse_connect import create_client
from clickhouse_connect.driver.client import Client
from clickhouse_connect.driver.exceptions import DatabaseError
from clickhouse_connect.driver.options import HAS_NUMPY, HAS_PANDAS
from clickhouse_connect.driver.query import QueryResult
from tests.integration_tests.conftest import TestConfig
Expand Down Expand Up @@ -139,3 +140,10 @@ def compare_rows(row_1, row_2):
def test_non_latin_query(test_client: Client):
result = test_client.query("SELECT database, name FROM system.tables WHERE engine_full IN ('空')")
assert len(result.result_set) == 0


def test_error_decode(test_client: Client):
try:
test_client.query("SELECT database, name FROM system.tables WHERE has_own_data = '空'")
except DatabaseError as ex:
assert '空' in str(ex)