1use std::fmt;
8
9pub type Result<T> = std::result::Result<T, ClientError>;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum ErrorCode {
15 UnsupportedScheme,
17 InvalidUri,
19 IoError,
21 QueryError,
23 FeatureDisabled,
25 ClientClosed,
27 Internal,
29 Network,
31 Protocol,
33 AuthRefused,
35 Engine,
37 NotFound,
39}
40
41impl ErrorCode {
42 pub fn as_str(self) -> &'static str {
43 match self {
44 ErrorCode::UnsupportedScheme => "UNSUPPORTED_SCHEME",
45 ErrorCode::InvalidUri => "INVALID_URI",
46 ErrorCode::IoError => "IO_ERROR",
47 ErrorCode::QueryError => "QUERY_ERROR",
48 ErrorCode::FeatureDisabled => "FEATURE_DISABLED",
49 ErrorCode::ClientClosed => "CLIENT_CLOSED",
50 ErrorCode::Internal => "INTERNAL_ERROR",
51 ErrorCode::Network => "NETWORK_ERROR",
52 ErrorCode::Protocol => "PROTOCOL_ERROR",
53 ErrorCode::AuthRefused => "AUTH_REFUSED",
54 ErrorCode::Engine => "ENGINE_ERROR",
55 ErrorCode::NotFound => "NOT_FOUND",
56 }
57 }
58}
59
60impl fmt::Display for ErrorCode {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 f.write_str(self.as_str())
63 }
64}
65
66#[derive(Debug, Clone)]
68pub struct ClientError {
69 pub code: ErrorCode,
70 pub message: String,
71}
72
73impl ClientError {
74 pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
75 Self {
76 code,
77 message: message.into(),
78 }
79 }
80
81 pub fn unsupported_scheme(scheme: impl AsRef<str>) -> Self {
82 Self::new(
83 ErrorCode::UnsupportedScheme,
84 format!(
85 "unsupported URI scheme: '{}'. Expected 'file://', 'memory://' or 'grpc://'.",
86 scheme.as_ref()
87 ),
88 )
89 }
90
91 pub fn feature_disabled(feature: &str) -> Self {
92 Self::new(
93 ErrorCode::FeatureDisabled,
94 format!(
95 "the '{feature}' feature is not enabled. Enable it in Cargo.toml: \
96 reddb-client = {{ version = \"…\", features = [\"{feature}\"] }}"
97 ),
98 )
99 }
100}
101
102impl fmt::Display for ClientError {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 write!(f, "[{}] {}", self.code, self.message)
105 }
106}
107
108impl std::error::Error for ClientError {}