vlorql_core/errors/kinds.rs
1//! Structured error categories used by the VlorQl core.
2//!
3//! The variants intentionally carry the values that caused validation to fail. This
4//! makes an error useful to both API consumers and an LLM that is asked to repair a
5//! query plan.
6
7use serde::{Deserialize, Serialize};
8use thiserror::Error;
9
10/// Errors found while validating an LLM-generated query plan.
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
12#[serde(rename_all = "snake_case")]
13pub enum ValidationErrorKind {
14 /// The LLM response could not be decoded as JSON.
15 #[error("the LLM response is not valid JSON")]
16 InvalidJson,
17 /// A required field was not present in the query plan.
18 #[error("required field `{field}` is missing")]
19 MissingField {
20 /// Name of the missing field, e.g. `"from"`.
21 field: String,
22 },
23 /// The query references a table that is not in the schema snapshot.
24 #[error("table `{table}` is not available")]
25 InvalidTable {
26 /// Name of the offending table.
27 table: String,
28 /// Tables the operator can choose from.
29 available_tables: Vec<String>,
30 },
31 /// The query references a column that is not in the selected table.
32 #[error("column `{column}` is not available on table `{table}`")]
33 InvalidColumn {
34 /// Owning table of the offending column.
35 table: String,
36 /// Name of the offending column.
37 column: String,
38 /// Columns the operator can choose from.
39 available_columns: Vec<String>,
40 },
41 /// The query uses a function outside the configured feature set.
42 #[error("function `{function}` is not allowed")]
43 InvalidFunction {
44 /// Name of the offending function.
45 function: String,
46 /// Functions the operator is allowed to call.
47 allowed_functions: Vec<String>,
48 },
49 /// An expression contains incompatible types.
50 #[error("type mismatch in `{expr}`: expected `{expected}`, found `{found}`")]
51 TypeMismatch {
52 /// The expected SQL type, as a human-readable string.
53 expected: String,
54 /// The actual SQL type the validator found.
55 found: String,
56 /// Expression whose type is wrong (for repair messages).
57 expr: String,
58 },
59 /// The query uses a feature disabled by the selected SQL dialect profile.
60 #[error("dialect feature `{feature}` is disabled")]
61 DialectFeatureDisabled {
62 /// Identifier of the disabled feature (e.g. `"cte"`).
63 feature: String,
64 },
65 /// The query exceeds the configured join limit.
66 #[error("query contains {actual} joins, but the maximum is {max}")]
67 TooManyJoins {
68 /// Number of joins present in the plan.
69 actual: usize,
70 /// Configured maximum.
71 max: usize,
72 },
73 /// Selected and grouped expressions do not satisfy aggregation rules.
74 #[error("aggregation mismatch: {message}")]
75 AggregationMismatch {
76 /// Human-readable description of the rule that was violated.
77 message: String,
78 },
79 /// Multiple validation errors occurred and could not be individually retried.
80 #[error("query plan has {count} validation error(s)")]
81 MultipleErrors {
82 /// Number of distinct validation errors.
83 count: usize,
84 },
85}
86
87/// Errors raised when a query violates an access policy.
88#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
89#[serde(rename_all = "snake_case")]
90pub enum PolicyErrorKind {
91 /// The query references a table denied by policy.
92 #[error("access to table `{table}` is denied")]
93 TableDenied {
94 /// Name of the denied table.
95 table: String,
96 },
97 /// The query references a column denied by policy.
98 #[error("access to column `{table}.{column}` is denied")]
99 ColumnDenied {
100 /// Owning table of the denied column.
101 table: String,
102 /// Name of the denied column.
103 column: String,
104 },
105}
106
107/// Errors raised while compiling a validated plan into SQL.
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
109#[serde(rename_all = "snake_case")]
110pub enum CompilationErrorKind {
111 /// A validated plan still contains a feature unsupported by the compiler.
112 #[error("dialect feature `{feature}` is not supported by the compiler")]
113 UnsupportedDialectFeature {
114 /// Identifier of the unsupported feature.
115 feature: String,
116 },
117 /// A generated parameter placeholder is not valid for the selected dialect.
118 #[error("parameter placeholder `{index}` is invalid")]
119 InvalidPlaceholder {
120 /// 1-based placeholder index that failed validation.
121 index: usize,
122 },
123}
124
125/// Errors caused by a schema that cannot satisfy a query plan.
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
127#[serde(rename_all = "snake_case")]
128pub enum SchemaErrorKind {
129 /// A referenced table does not exist in the schema snapshot.
130 #[error("table `{table}` was not found in the schema")]
131 TableNotFound {
132 /// Name of the missing table.
133 table: String,
134 },
135 /// A referenced column does not exist on a table in the schema snapshot.
136 #[error("column `{table}.{column}` was not found in the schema")]
137 ColumnNotFound {
138 /// Owning table of the missing column.
139 table: String,
140 /// Name of the missing column.
141 column: String,
142 },
143}
144
145/// Errors returned by an LLM provider or while decoding its response.
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
147#[serde(rename_all = "snake_case")]
148pub enum LlmErrorKind {
149 /// The provider returned an unsuccessful HTTP response.
150 #[error("LLM API returned HTTP {status}: {message}")]
151 ApiError {
152 /// HTTP status code returned by the provider.
153 status: u16,
154 /// Provider-supplied error message.
155 message: String,
156 },
157 /// The provider did not respond before the configured deadline.
158 #[error("LLM request timed out")]
159 Timeout,
160 /// The provider response could not be decoded into the requested plan.
161 #[error("LLM response could not be parsed: {details}")]
162 ParseError {
163 /// Human-readable description of the parse failure.
164 details: String,
165 },
166}
167
168/// Errors caused by an incomplete or invalid VlorQl configuration.
169#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
170#[serde(rename_all = "snake_case")]
171pub enum ConfigErrorKind {
172 /// No LLM client was configured for an operation that requires one.
173 #[error("an LLM client has not been configured")]
174 MissingLlmClient,
175 /// No schema snapshot was configured for an operation that requires one.
176 #[error("a schema snapshot has not been configured")]
177 MissingSchema,
178 /// The configured SQL dialect is not recognized.
179 #[error("SQL dialect `{dialect}` is invalid")]
180 InvalidDialect {
181 /// The unrecognized dialect identifier.
182 dialect: String,
183 },
184 /// An API key is required but was not provided.
185 #[error("API key is required for provider `{provider}`")]
186 MissingApiKey {
187 /// Name of the provider that requires a key.
188 provider: String,
189 },
190 /// The model name is empty or whitespace-only.
191 #[error("model name must not be empty")]
192 EmptyModel,
193 /// A statistics or configuration file could not be read or parsed.
194 #[error("failed to load configuration file `{path}`: {reason}")]
195 ConfigFileError {
196 /// Filesystem path of the offending file.
197 path: String,
198 /// Human-readable description of the read or parse failure.
199 reason: String,
200 },
201}