Skip to main content

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 table exists in the schema but is not part of the
136    /// query's `FROM` or `JOIN` clauses.
137    #[error(
138        "table `{table}` exists in the schema but is not referenced in the FROM or JOIN clauses of the query"
139    )]
140    TableNotInScope {
141        /// Name of the table that is missing from the query scope.
142        table: String,
143    },
144    /// A referenced column does not exist on a table in the schema snapshot.
145    #[error("column `{table}.{column}` was not found in the schema")]
146    ColumnNotFound {
147        /// Owning table of the missing column.
148        table: String,
149        /// Name of the missing column.
150        column: String,
151    },
152}
153
154/// Errors returned by an LLM provider or while decoding its response.
155#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
156#[serde(rename_all = "snake_case")]
157pub enum LlmErrorKind {
158    /// The provider returned an unsuccessful HTTP response.
159    #[error("LLM API returned HTTP {status}: {message}")]
160    ApiError {
161        /// HTTP status code returned by the provider.
162        status: u16,
163        /// Provider-supplied error message.
164        message: String,
165    },
166    /// The provider did not respond before the configured deadline.
167    #[error("LLM request timed out")]
168    Timeout,
169    /// The provider response could not be decoded into the requested plan.
170    #[error("LLM response could not be parsed: {details}")]
171    ParseError {
172        /// Human-readable description of the parse failure.
173        details: String,
174    },
175}
176
177/// Errors raised by the SQL-injection audit stage.
178#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
179#[serde(rename_all = "snake_case")]
180pub enum AuditErrorKind {
181    /// A referenced identifier (table or column) was not found in the schema.
182    #[error("identifier `{identifier}` not found in schema ({context})")]
183    IdentifierNotFound {
184        /// The identifier that could not be resolved.
185        identifier: String,
186        /// Human-readable context describing where the lookup failed.
187        context: String,
188    },
189    /// An identifier contains a known SQL-injection pattern.
190    #[error("identifier `{identifier}` contains suspicious pattern `{pattern}`")]
191    SuspiciousPattern {
192        /// The identifier that triggered the pattern match.
193        identifier: String,
194        /// The matched suspicious pattern (e.g. `";"`, `"DROP "`).
195        pattern: String,
196    },
197}
198
199/// Errors caused by an incomplete or invalid VlorQl configuration.
200#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
201#[serde(rename_all = "snake_case")]
202pub enum ConfigErrorKind {
203    /// No LLM client was configured for an operation that requires one.
204    #[error("an LLM client has not been configured")]
205    MissingLlmClient,
206    /// No schema snapshot was configured for an operation that requires one.
207    #[error("a schema snapshot has not been configured")]
208    MissingSchema,
209    /// The configured SQL dialect is not recognized.
210    #[error("SQL dialect `{dialect}` is invalid")]
211    InvalidDialect {
212        /// The unrecognized dialect identifier.
213        dialect: String,
214    },
215    /// An API key is required but was not provided.
216    #[error("API key is required for provider `{provider}`")]
217    MissingApiKey {
218        /// Name of the provider that requires a key.
219        provider: String,
220    },
221    /// The model name is empty or whitespace-only.
222    #[error("model name must not be empty")]
223    EmptyModel,
224    /// A statistics or configuration file could not be read or parsed.
225    #[error("failed to load configuration file `{path}`: {reason}")]
226    ConfigFileError {
227        /// Filesystem path of the offending file.
228        path: String,
229        /// Human-readable description of the read or parse failure.
230        reason: String,
231    },
232    /// An internal operation failed (e.g. a blocking task join error).
233    #[error("internal error: {reason}")]
234    InternalError {
235        /// Human-readable description of the failure.
236        reason: String,
237    },
238}