use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("SSH error: {0}")]
Ssh(#[from] lmrc_ssh::Error),
#[error("SSH command execution failed: {message}")]
SshExecution {
message: String,
command: String,
},
#[error("PostgreSQL installation failed: {0}")]
Installation(String),
#[error("PostgreSQL configuration failed: {0}")]
Configuration(String),
#[error("PostgreSQL is not installed on the server")]
NotInstalled,
#[error("PostgreSQL version {0} is already installed")]
AlreadyInstalled(String),
#[error("Invalid PostgreSQL version: {0}")]
InvalidVersion(String),
#[error("Invalid configuration parameter: {parameter} = {value}")]
InvalidConfig {
parameter: String,
value: String,
},
#[error("Missing required configuration: {0}")]
MissingConfig(String),
#[error("PostgreSQL service error: {0}")]
ServiceError(String),
#[error("Database connection test failed: {0}")]
ConnectionTest(String),
#[error("PostgreSQL uninstallation failed: {0}")]
Uninstallation(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("{0}")]
Other(String),
}
impl Error {
pub fn ssh_execution(message: impl Into<String>, command: impl Into<String>) -> Self {
Self::SshExecution {
message: message.into(),
command: command.into(),
}
}
pub fn invalid_config(parameter: impl Into<String>, value: impl Into<String>) -> Self {
Self::InvalidConfig {
parameter: parameter.into(),
value: value.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = Error::NotInstalled;
assert_eq!(err.to_string(), "PostgreSQL is not installed on the server");
let err = Error::AlreadyInstalled("15".to_string());
assert_eq!(
err.to_string(),
"PostgreSQL version 15 is already installed"
);
let err = Error::invalid_config("max_connections", "invalid");
assert_eq!(
err.to_string(),
"Invalid configuration parameter: max_connections = invalid"
);
}
#[test]
fn test_ssh_execution_error() {
let err = Error::ssh_execution("command failed", "apt-get install");
match err {
Error::SshExecution { message, command } => {
assert_eq!(message, "command failed");
assert_eq!(command, "apt-get install");
}
_ => panic!("Wrong error type"),
}
}
}