clickhouse_arrow/
errors.rs

1use std::borrow::Cow;
2use std::num::TryFromIntError;
3use std::str::Utf8Error;
4use std::string::FromUtf8Error;
5
6use crate::Type;
7use crate::native::ServerError;
8
9/// Represents various library errors
10#[derive(thiserror::Error, Debug)]
11pub enum Error {
12    #[error("io error: {0}")]
13    Io(#[from] std::io::Error),
14
15    #[error("can't fetch the same column twice from RawRow")]
16    DoubleFetch,
17    #[error("column index was out of bounds or not present")]
18    OutOfBounds,
19    #[error("missing field {0}")]
20    MissingField(&'static str),
21    #[error("missing connection information")]
22    MissingConnectionInformation,
23    #[error("malformed connection information: {0}")]
24    MalformedConnectionInformation(String),
25    #[error("duplicate field {0} in struct")]
26    DuplicateField(&'static str),
27    #[error("protocol error: {0}")]
28    Protocol(String),
29    #[error("Internal channel closed")]
30    InternalChannelError,
31    #[error("connection timeout: {0}")]
32    ConnectionTimeout(String),
33    #[error("connection gone: reason = {0}")]
34    ConnectionGone(&'static str),
35    #[error("type parse error: {0}")]
36    TypeParseError(String),
37    #[error("deserialize error: {0}")]
38    DeserializeError(String),
39    #[error("serialize error: {0}")]
40    SerializeError(String),
41    #[error("deserialize error for column {0}: {1}")]
42    DeserializeErrorWithColumn(&'static str, String),
43    #[error("connection startup error")]
44    StartupError,
45    #[error("Exception({0:?})")]
46    ServerException(ServerError),
47    #[error("unexpected type: {0}")]
48    UnexpectedType(Type),
49    #[error("unexpected type for column {0}: {1}")]
50    UnexpectedTypeWithColumn(Cow<'static, str>, Type),
51    #[error("type conversion failure: {0}")]
52    TypeConversion(String),
53    #[error("str utf-8 conversion error: {0}")]
54    Utf8(#[from] Utf8Error),
55    #[error("string utf-8 conversion error: {0}")]
56    FromUtf8(#[from] FromUtf8Error),
57    #[error("Date failed to parse: {0}")]
58    DateTime(#[from] TryFromIntError),
59    #[error("channel closed")]
60    ChannelClosed,
61    #[error("Timeout while sending message: {0}")]
62    OutgoingTimeout(String),
63    #[error("Invalid DNS name: {0}")]
64    InvalidDnsName(String),
65    #[error("Unsupported setting type: {0}")]
66    UnsupportedSettingType(String),
67    #[error("Unsupported setting field type: {0}")]
68    UnsupportedFieldType(String),
69    #[error("No schemas found")]
70    UndefinedSchemas,
71    #[error("Tables undefined in database {db}: {tables:?}")]
72    UndefinedTables { db: String, tables: Vec<String> },
73    #[error("Schema configuration is not valid: {0}")]
74    SchemaConfig(String),
75    #[error("DDL Statement malformed: {0}")]
76    DDLMalformed(String),
77    #[error("Insufficient scope for ddl queries: {0}")]
78    InsufficientDDLScope(String),
79    #[error("Client error: {0}")]
80    Client(String),
81
82    // Other
83    #[error("External error: {0}")]
84    External(Box<dyn std::error::Error + Send + Sync>),
85    #[error("Unknown error occurred: {0}")]
86    Unknown(String),
87
88    // Arrow errors
89    #[error(transparent)]
90    Arrow(#[from] arrow::error::ArrowError),
91    #[error("insert block retry")]
92    InsertArrowRetry(arrow::record_batch::RecordBatch),
93    #[error("arrow serialize error: {0}")]
94    ArrowSerialize(String),
95    #[error("arrow deserialize error: {0}")]
96    ArrowDeserialize(String),
97    #[error("Type mismatch: expected {expected}")]
98    ArrowTypeMismatch { expected: String, provided: String },
99    #[error("Unsupported arrow type: {0}")]
100    ArrowUnsupportedType(String),
101
102    // RowBinary
103    #[error(transparent)]
104    BytesRead(#[from] bytes::TryGetError),
105}
106
107impl Error {
108    #[must_use]
109    pub fn with_column_name(self, name: &'static str) -> Self {
110        match self {
111            Error::DeserializeError(e) => Error::DeserializeErrorWithColumn(name, e),
112            Error::UnexpectedType(e) => Error::UnexpectedTypeWithColumn(Cow::Borrowed(name), e),
113            x => x,
114        }
115    }
116}
117
118pub type Result<T, E = Error> = std::result::Result<T, E>;