Skip to main content

database_mcp_sql/
error.rs

1//! SQL-layer error types for validation, timeout, and query failures.
2
3/// Errors produced by SQL validation, identifier checking, and query execution.
4#[derive(Debug, thiserror::Error)]
5pub enum SqlError {
6    /// Query blocked by read-only mode.
7    #[error("Query blocked: only SELECT, SHOW, DESC, DESCRIBE, USE queries are allowed in read-only mode")]
8    ReadOnlyViolation,
9
10    /// `LOAD_FILE()` function blocked for security.
11    #[error("Operation forbidden: LOAD_FILE() is not allowed for security reasons")]
12    LoadFileBlocked,
13
14    /// INTO OUTFILE/DUMPFILE blocked for security.
15    #[error("Operation forbidden: SELECT INTO OUTFILE/DUMPFILE is not allowed for security reasons")]
16    IntoOutfileBlocked,
17
18    /// Multiple SQL statements blocked.
19    #[error("Query blocked: only single statements are allowed")]
20    MultiStatement,
21
22    /// Invalid database or table name identifier.
23    #[error("Invalid identifier '{0}': must not be empty, whitespace-only, or contain control characters")]
24    InvalidIdentifier(String),
25
26    /// Query exceeded the configured timeout.
27    #[error("Query timed out after {elapsed_secs:.1}s: {sql}")]
28    QueryTimeout {
29        /// Wall-clock seconds elapsed before cancellation.
30        elapsed_secs: f64,
31        /// The SQL statement that was cancelled.
32        sql: String,
33    },
34
35    /// Database query execution failed.
36    #[error("Database error: {0}")]
37    Query(String),
38
39    /// Table not found in database.
40    #[error("Table not found: {0}")]
41    TableNotFound(String),
42}
43
44impl From<SqlError> for rmcp::model::ErrorData {
45    fn from(e: SqlError) -> Self {
46        Self::internal_error(e.to_string(), None)
47    }
48}