lmrc_postgres/
error.rs

1//! Error types for PostgreSQL Manager
2//!
3//! This module provides comprehensive error types for all operations.
4
5use thiserror::Error;
6
7/// Result type alias for PostgreSQL Manager operations
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// Error types for PostgreSQL Manager operations
11#[derive(Error, Debug)]
12pub enum Error {
13    /// SSH error
14    #[error("SSH error: {0}")]
15    Ssh(#[from] lmrc_ssh::Error),
16
17    /// SSH command execution error
18    #[error("SSH command execution failed: {message}")]
19    SshExecution {
20        /// Error message
21        message: String,
22        /// Command that failed
23        command: String,
24    },
25
26    /// PostgreSQL installation error
27    #[error("PostgreSQL installation failed: {0}")]
28    Installation(String),
29
30    /// PostgreSQL configuration error
31    #[error("PostgreSQL configuration failed: {0}")]
32    Configuration(String),
33
34    /// PostgreSQL not installed
35    #[error("PostgreSQL is not installed on the server")]
36    NotInstalled,
37
38    /// PostgreSQL already installed
39    #[error("PostgreSQL version {0} is already installed")]
40    AlreadyInstalled(String),
41
42    /// Invalid PostgreSQL version
43    #[error("Invalid PostgreSQL version: {0}")]
44    InvalidVersion(String),
45
46    /// Invalid configuration parameter
47    #[error("Invalid configuration parameter: {parameter} = {value}")]
48    InvalidConfig {
49        /// Parameter name
50        parameter: String,
51        /// Invalid value
52        value: String,
53    },
54
55    /// Missing required configuration
56    #[error("Missing required configuration: {0}")]
57    MissingConfig(String),
58
59    /// PostgreSQL service error
60    #[error("PostgreSQL service error: {0}")]
61    ServiceError(String),
62
63    /// Database connection test failed
64    #[error("Database connection test failed: {0}")]
65    ConnectionTest(String),
66
67    /// Uninstallation error
68    #[error("PostgreSQL uninstallation failed: {0}")]
69    Uninstallation(String),
70
71    /// IO error
72    #[error("IO error: {0}")]
73    Io(#[from] std::io::Error),
74
75    /// Serialization error
76    #[error("Serialization error: {0}")]
77    Serialization(#[from] serde_json::Error),
78
79    /// Generic error
80    #[error("{0}")]
81    Other(String),
82}
83
84impl Error {
85    /// Create a new SSH execution error
86    pub fn ssh_execution(message: impl Into<String>, command: impl Into<String>) -> Self {
87        Self::SshExecution {
88            message: message.into(),
89            command: command.into(),
90        }
91    }
92
93    /// Create a new invalid config error
94    pub fn invalid_config(parameter: impl Into<String>, value: impl Into<String>) -> Self {
95        Self::InvalidConfig {
96            parameter: parameter.into(),
97            value: value.into(),
98        }
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn test_error_display() {
108        let err = Error::NotInstalled;
109        assert_eq!(err.to_string(), "PostgreSQL is not installed on the server");
110
111        let err = Error::AlreadyInstalled("15".to_string());
112        assert_eq!(
113            err.to_string(),
114            "PostgreSQL version 15 is already installed"
115        );
116
117        let err = Error::invalid_config("max_connections", "invalid");
118        assert_eq!(
119            err.to_string(),
120            "Invalid configuration parameter: max_connections = invalid"
121        );
122    }
123
124    #[test]
125    fn test_ssh_execution_error() {
126        let err = Error::ssh_execution("command failed", "apt-get install");
127        match err {
128            Error::SshExecution { message, command } => {
129                assert_eq!(message, "command failed");
130                assert_eq!(command, "apt-get install");
131            }
132            _ => panic!("Wrong error type"),
133        }
134    }
135}