Skip to main content

database_mcp/
error.rs

1//! Application error types for the MCP server.
2//!
3//! Defines [`AppError`] with variants for connection, security validation,
4//! and query execution failures. Configuration errors live in the
5//! [`config`](crate::config) module.
6
7/// Errors that can occur during MCP server operation.
8#[derive(Debug, thiserror::Error)]
9#[allow(dead_code)]
10pub enum AppError {
11    /// Database connection failed.
12    #[error("Database connection error: {0}")]
13    Connection(String),
14
15    /// Query blocked by read-only mode.
16    #[error("Query blocked: only SELECT, SHOW, DESC, DESCRIBE, USE queries are allowed in read-only mode")]
17    ReadOnlyViolation,
18
19    /// `LOAD_FILE()` function blocked for security.
20    #[error("Operation forbidden: LOAD_FILE() is not allowed for security reasons")]
21    LoadFileBlocked,
22
23    /// INTO OUTFILE/DUMPFILE blocked for security.
24    #[error("Operation forbidden: SELECT INTO OUTFILE/DUMPFILE is not allowed for security reasons")]
25    IntoOutfileBlocked,
26
27    /// Multiple SQL statements blocked.
28    #[error("Query blocked: only single statements are allowed")]
29    MultiStatement,
30
31    /// Invalid database or table name identifier.
32    #[error("Invalid identifier '{0}': must not be empty, whitespace-only, or contain control characters")]
33    InvalidIdentifier(String),
34
35    /// Database query execution failed.
36    #[error("Database error: {0}")]
37    Query(String),
38
39    /// Table isn't found in database.
40    #[error("Table not found: {0}")]
41    TableNotFound(String),
42}
43
44impl From<sqlx::Error> for AppError {
45    fn from(e: sqlx::Error) -> Self {
46        AppError::Connection(e.to_string())
47    }
48}