Skip to content
Open
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
37 changes: 37 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ pub enum Error {
Custom(String),
#[error("bad response: {0}")]
BadResponse(String),
/// A typed ClickHouse server exception, parsed from the server's response.
///
/// Constructed when the response body or `X-ClickHouse-Exception-Code`
/// header carries a recognisable `Code: <num>. DB::<Class>: <message>
/// (NAME) (version ...)` payload. Falls back to [`Error::BadResponse`]
/// for responses that cannot be parsed (proxy errors, non-ClickHouse
/// HTTP responses, malformed payloads).
///
/// Use this variant for code/name-aware control flow:
///
/// ```ignore
/// match err {
/// Error::ServerError { code: 252, .. } => alert_on_too_many_parts(),
/// Error::ServerError { code, .. } if (43..=53).contains(&code) =>
/// report_caller_error(),
/// _ => report_internal_error(err),
/// }
/// ```
#[error("server error: Code: {code}{}: {message}", name.as_deref().map(|n| format!(" ({n})")).unwrap_or_default())]
ServerError {
/// Numeric ClickHouse exception code (e.g. `252` for `TOO_MANY_PARTS`).
///
/// The full table of codes lives in
/// [src/Common/ErrorCodes.cpp](https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/ErrorCodes.cpp)
/// in the ClickHouse server tree.
code: u32,
/// Human-readable exception name (e.g. `"TOO_MANY_PARTS"`), extracted
/// from the trailing parenthesised tag in the server message.
///
/// `None` when the response is the bare `Code: NNN` form (e.g. a
/// header-only fast-fail) or when the trailing tag is absent.
name: Option<String>,
/// The exception message — the text between `DB::<Class>:` and the
/// trailing `(NAME) (version ...)` metadata, trimmed.
message: String,
Comment on lines +62 to +76

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.

I'm starting to think that structured errors like this should be their own structs. That allows for more flexible handling, as you can write functions which handle that error type specifically.

},
#[error("timeout expired")]
TimedOut,
#[error("error while parsing columns header from the response: {0}")]
Expand Down Expand Up @@ -130,6 +166,7 @@ impl Error {
Error::VariantDiscriminatorIsOutOfBound(_) => "VariantDiscriminatorIsOutOfBound",
Error::Custom(_) => "Custom",
Error::BadResponse(_) => "BadResponse",
Error::ServerError { .. } => "ServerError",
Error::TimedOut => "TimedOut",
Error::InvalidColumnsHeader(_) => "InvalidColumnsHeader",
Error::SchemaMismatch(_) => "SchemaMismatch",
Expand Down
Loading
Loading