Skip to content

expose ClickHouse error code and name on raised exceptions#788

Merged
joe-clickhouse merged 5 commits into
mainfrom
joe/786-http-errors-do-not-expose-structured-clickhouse-error-codename-only-an-ad-hoc-message-string
Jun 10, 2026
Merged

expose ClickHouse error code and name on raised exceptions#788
joe-clickhouse merged 5 commits into
mainfrom
joe/786-http-errors-do-not-expose-structured-clickhouse-error-codename-only-an-ad-hoc-message-string

Conversation

@joe-clickhouse

Copy link
Copy Markdown
Contributor

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-Code header and discarded.

  • Add code and name attributes to the Error base class, inherited by DatabaseError and OperationalError. Callers can now branch on exc.code == 60 instead of parsing the message.
  • Populate code from the exception header in both the sync and async error handlers. It is set even when show_clickhouse_errors is disabled, since the numeric code is not sensitive.
  • Populate name with the symbolic error name such as UNKNOWN_TABLE, parsed from the response body and gated behind show_clickhouse_errors. It is read from the full body before truncation so a small max_error_size does not drop it.
  • The human readable message string is unchanged. Both fields default to None when unavailable, for example on transport errors.

Closes #786

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_setting helpers.

Changes:

  • Add code (numeric) and name (symbolic) attributes to the Error base exception, inherited by DatabaseError / OperationalError.
  • Populate code from X-ClickHouse-Exception-Code in both sync and async HTTP error handlers; populate name from the response body when show_clickhouse_errors is 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.

Comment thread tests/unit_tests/test_driver/test_httpclient.py Outdated
…-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
@joe-clickhouse joe-clickhouse merged commit aaddb19 into main Jun 10, 2026
34 of 56 checks passed
@joe-clickhouse joe-clickhouse deleted the joe/786-http-errors-do-not-expose-structured-clickhouse-error-codename-only-an-ad-hoc-message-string branch June 10, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP errors do not expose structured ClickHouse error code/name; only an ad-hoc message string

2 participants