questdb/
error.rs

1use std::fmt::{Display, Formatter};
2
3macro_rules! fmt {
4    ($code:ident, $($arg:tt)*) => {
5        crate::error::Error::new(
6            crate::error::ErrorCode::$code,
7            format!($($arg)*))
8    }
9}
10
11/// Category of error.
12///
13/// Accessible via Error's [`code`](Error::code) method.
14#[derive(Debug, Copy, Clone, PartialEq)]
15pub enum ErrorCode {
16    /// The host, port, or interface was incorrect.
17    CouldNotResolveAddr,
18
19    /// Called methods in the wrong order. E.g. `symbol` after `column`.
20    InvalidApiCall,
21
22    /// A network error connecting or flushing data out.
23    SocketError,
24
25    /// The string or symbol field is not encoded in valid UTF-8.
26    ///
27    /// *This error is reserved for the
28    /// [C and C++ API](https://github.com/questdb/c-questdb-client/).*
29    InvalidUtf8,
30
31    /// The table name or column name contains bad characters.
32    InvalidName,
33
34    /// The supplied timestamp is invalid.
35    InvalidTimestamp,
36
37    /// Error during the authentication process.
38    AuthError,
39
40    /// Error during TLS handshake.
41    TlsError,
42
43    /// The server does not support ILP-over-HTTP.
44    HttpNotSupported,
45
46    /// Error sent back from the server during flush.
47    ServerFlushError,
48
49    /// Bad configuration.
50    ConfigError,
51}
52
53/// An error that occurred when using QuestDB client library.
54#[derive(Debug, PartialEq)]
55pub struct Error {
56    code: ErrorCode,
57    msg: String,
58}
59
60impl Error {
61    /// Create an error with the given code and message.
62    pub fn new<S: Into<String>>(code: ErrorCode, msg: S) -> Error {
63        Error {
64            code,
65            msg: msg.into(),
66        }
67    }
68
69    /// Get the error code (category) of this error.
70    pub fn code(&self) -> ErrorCode {
71        self.code
72    }
73
74    /// Get the string message of this error.
75    pub fn msg(&self) -> &str {
76        &self.msg
77    }
78}
79
80impl Display for Error {
81    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
82        f.write_str(&self.msg)
83    }
84}
85
86impl std::error::Error for Error {}
87
88/// A specialized `Result` type for the crate's [`Error`] type.
89pub type Result<T> = std::result::Result<T, Error>;
90
91pub(crate) use fmt;