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 ParamsUnsupported,
41 InvalidArgument,
43 InvalidResponse,
45}
46
47impl ErrorCode {
48 pub fn as_str(self) -> &'static str {
49 match self {
50 ErrorCode::UnsupportedScheme => "UNSUPPORTED_SCHEME",
51 ErrorCode::InvalidUri => "INVALID_URI",
52 ErrorCode::IoError => "IO_ERROR",
53 ErrorCode::QueryError => "QUERY_ERROR",
54 ErrorCode::FeatureDisabled => "FEATURE_DISABLED",
55 ErrorCode::ClientClosed => "CLIENT_CLOSED",
56 ErrorCode::Internal => "INTERNAL_ERROR",
57 ErrorCode::Network => "NETWORK_ERROR",
58 ErrorCode::Protocol => "PROTOCOL_ERROR",
59 ErrorCode::AuthRefused => "AUTH_REFUSED",
60 ErrorCode::Engine => "ENGINE_ERROR",
61 ErrorCode::NotFound => "NOT_FOUND",
62 ErrorCode::ParamsUnsupported => "PARAMS_UNSUPPORTED",
63 ErrorCode::InvalidArgument => "INVALID_ARGUMENT",
64 ErrorCode::InvalidResponse => "INVALID_RESPONSE",
65 }
66 }
67}
68
69impl fmt::Display for ErrorCode {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.write_str(self.as_str())
72 }
73}
74
75#[derive(Debug, Clone)]
77pub struct ClientError {
78 pub code: ErrorCode,
79 pub message: String,
80}
81
82impl ClientError {
83 pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
84 Self {
85 code,
86 message: message.into(),
87 }
88 }
89
90 pub fn unsupported_scheme(scheme: impl AsRef<str>) -> Self {
91 Self::new(
92 ErrorCode::UnsupportedScheme,
93 format!(
94 "unsupported URI scheme: '{}'. Expected 'file://', 'memory://' or 'grpc://'.",
95 scheme.as_ref()
96 ),
97 )
98 }
99
100 pub fn feature_disabled(feature: &str) -> Self {
101 Self::new(
102 ErrorCode::FeatureDisabled,
103 format!(
104 "the '{feature}' feature is not enabled. Enable it in Cargo.toml: \
105 reddb-io-client = {{ version = \"…\", features = [\"{feature}\"] }}"
106 ),
107 )
108 }
109}
110
111impl fmt::Display for ClientError {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 write!(f, "[{}] {}", self.code, self.message)
114 }
115}
116
117impl std::error::Error for ClientError {}