1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Error types for QAIL.
/// Error types for QAIL operations.
#[derive(Debug)]
pub enum QailError {
/// Failed to parse the QAIL query string.
Parse {
/// Byte offset of the error.
position: usize,
/// Human-readable error message.
message: String,
},
/// Invalid action (must be get, set, del, or add).
InvalidAction(String),
/// Required syntax symbol is missing.
MissingSymbol {
/// The missing symbol.
symbol: &'static str,
/// Description of the expected symbol.
description: &'static str,
},
/// Invalid operator in expression.
InvalidOperator(String),
/// Invalid value in expression.
InvalidValue(String),
/// Database-layer error.
Database(String),
/// Connection-layer error.
Connection(String),
/// Execution-layer error.
Execution(String),
/// Validation error.
Validation(String),
/// Configuration error.
Config(String),
/// I/O error.
Io(std::io::Error),
}
impl QailError {
/// Create a parse error at the given position.
pub fn parse(position: usize, message: impl Into<String>) -> Self {
Self::Parse {
position,
message: message.into(),
}
}
/// Create a missing symbol error.
pub fn missing(symbol: &'static str, description: &'static str) -> Self {
Self::MissingSymbol {
symbol,
description,
}
}
}
impl std::fmt::Display for QailError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Parse { position, message } => {
write!(f, "Parse error at position {position}: {message}")
}
Self::InvalidAction(action) => {
write!(
f,
"Invalid action: '{action}'. Expected: get, set, del, or add"
)
}
Self::MissingSymbol {
symbol,
description,
} => {
write!(f, "Missing required symbol: {symbol} ({description})")
}
Self::InvalidOperator(op) => write!(f, "Invalid operator: '{op}'"),
Self::InvalidValue(value) => write!(f, "Invalid value: {value}"),
Self::Database(msg) => write!(f, "Database error: {msg}"),
Self::Connection(msg) => write!(f, "Connection error: {msg}"),
Self::Execution(msg) => write!(f, "Execution error: {msg}"),
Self::Validation(msg) => write!(f, "Validation error: {msg}"),
Self::Config(msg) => write!(f, "Configuration error: {msg}"),
Self::Io(err) => write!(f, "IO error: {err}"),
}
}
}
impl std::error::Error for QailError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for QailError {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
/// Result type alias for QAIL operations.
pub type QailResult<T> = Result<T, QailError>;
/// Error type for query-builder operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QailBuildError {
/// RLS insertion cannot safely align positional values without columns.
RlsInsertRequiresExplicitColumns {
/// Target table being scoped.
table: String,
/// Tenant column that would be injected.
tenant_column: String,
},
/// RLS-protected updates cannot rewrite the tenant column.
RlsTenantColumnMutationDenied {
/// Target table being scoped.
table: String,
/// Tenant column that was assigned.
tenant_column: String,
},
/// RLS-protected MERGE query sources need a tenant projection for safe row classification.
RlsMergeSourceTenantProjectionRequired {
/// Target table being scoped.
table: String,
/// Tenant column that must be projected by the source query.
tenant_column: String,
},
/// Runtime relation registry lock failed.
RelationRegistryLock(String),
/// Relation metadata has more than one possible join edge.
AmbiguousRelation {
/// Source table.
from_table: String,
/// Related table.
to_table: String,
/// Number of registered foreign-key candidates.
foreign_key_count: usize,
},
/// No schema relation could be found for an implicit join.
RelationNotFound {
/// Current table.
from_table: String,
/// Requested related table.
to_table: String,
},
}
impl std::fmt::Display for QailBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::RlsInsertRequiresExplicitColumns {
table,
tenant_column,
} => write!(
f,
"with_rls requires explicit columns for positional INSERT payloads on table '{table}' (tenant column '{tenant_column}')"
),
Self::RlsTenantColumnMutationDenied {
table,
tenant_column,
} => write!(
f,
"with_rls rejects tenant column mutation on table '{table}' (tenant column '{tenant_column}')"
),
Self::RlsMergeSourceTenantProjectionRequired {
table,
tenant_column,
} => write!(
f,
"with_rls requires MERGE query sources for table '{table}' to project tenant column '{tenant_column}'"
),
Self::RelationRegistryLock(msg) => write!(f, "Relation registry lock error: {msg}"),
Self::AmbiguousRelation {
from_table,
to_table,
foreign_key_count,
} => write!(
f,
"Ambiguous relation between '{from_table}' and '{to_table}': {foreign_key_count} foreign keys registered. Use an explicit join condition."
),
Self::RelationNotFound {
from_table,
to_table,
} => write!(
f,
"No relation found between '{from_table}' and '{to_table}'. Define a ref: in schema.qail or use load_schema_relations() first."
),
}
}
}
impl std::error::Error for QailBuildError {}
/// Result type alias for query-builder operations.
pub type QailBuildResult<T> = Result<T, QailBuildError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = QailError::parse(5, "unexpected character");
assert_eq!(
err.to_string(),
"Parse error at position 5: unexpected character"
);
}
}