expose ClickHouse error code and name on raised exceptions#788
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the driver’s exception model so ClickHouse server-side HTTP errors expose structured metadata (code and name) on raised exceptions, avoiding brittle regex parsing of formatted message strings.
Findings (ordered by severity):
- Minor test maintainability nit: one unit test mutates a private settings dict directly instead of using the public
common.get_setting/common.set_settinghelpers.
Changes:
- Add
code(numeric) andname(symbolic) attributes to theErrorbase exception, inherited byDatabaseError/OperationalError. - Populate
codefromX-ClickHouse-Exception-Codein both sync and async HTTP error handlers; populatenamefrom the response body whenshow_clickhouse_errorsis enabled. - Add unit + integration tests covering code/name extraction and truncation behavior; document the feature in the changelog.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
clickhouse_connect/driver/exceptions.py |
Adds code/name fields on the base error type plus helpers to parse code/name from header/body. |
clickhouse_connect/driver/httpclient.py |
Sync HTTP error handler now parses and attaches structured code/name to raised exceptions. |
clickhouse_connect/driver/asyncclient.py |
Async HTTP error handler now parses and attaches structured code/name to raised exceptions. |
tests/unit_tests/test_driver/test_httpclient.py |
Adds sync/async unit tests asserting structured fields (and truncation behavior). |
tests/unit_tests/test_driver/test_exceptions.py |
Adds focused unit tests for parsing helpers and exception field defaults. |
tests/integration_tests/test_client.py |
Adds integration coverage verifying structured fields are present on real server errors. |
CHANGELOG.md |
Documents the new structured error fields and their gating behavior. |
…-clickhouse-error-codename-only-an-ad-hoc-message-string
…-clickhouse-error-codename-only-an-ad-hoc-message-string
…-clickhouse-error-codename-only-an-ad-hoc-message-string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Server errors only carried a formatted message string, so callers had to regex-parse it to branch on the ClickHouse error code. The code was already available in the
X-ClickHouse-Exception-Codeheader and discarded.codeandnameattributes to theErrorbase class, inherited byDatabaseErrorandOperationalError. Callers can now branch onexc.code == 60instead of parsing the message.codefrom the exception header in both the sync and async error handlers. It is set even whenshow_clickhouse_errorsis disabled, since the numeric code is not sensitive.namewith the symbolic error name such asUNKNOWN_TABLE, parsed from the response body and gated behindshow_clickhouse_errors. It is read from the full body before truncation so a smallmax_error_sizedoes not drop it.Nonewhen unavailable, for example on transport errors.Closes #786
Checklist
Delete items not relevant to your PR: