use thiserror::Error;
#[derive(Debug, Error)]
pub enum MgError {
#[error("Connection error: {0}")]
Connection(String),
#[error("Query execution error: {0}")]
QueryExecution(String),
#[error("Invalid parameter '{parameter}': {reason}")]
InvalidParameter { parameter: String, reason: String },
#[error("Parameter '{parameter}' contains null byte")]
NullByte { parameter: String },
#[error("Invalid timestamp values")]
InvalidTimestamp,
#[error("Invalid connection state: {operation} cannot be called while {state}")]
InvalidState { operation: String, state: String },
#[error("FFI error: {0}")]
Ffi(String),
}
impl MgError {
pub fn connection(message: impl Into<String>) -> Self {
MgError::Connection(message.into())
}
pub fn query(message: impl Into<String>) -> Self {
MgError::QueryExecution(message.into())
}
pub fn invalid_parameter(parameter: impl Into<String>, reason: impl Into<String>) -> Self {
MgError::InvalidParameter {
parameter: parameter.into(),
reason: reason.into(),
}
}
pub fn null_byte(parameter: impl Into<String>) -> Self {
MgError::NullByte {
parameter: parameter.into(),
}
}
pub fn invalid_state(operation: impl Into<String>, state: impl Into<String>) -> Self {
MgError::InvalidState {
operation: operation.into(),
state: state.into(),
}
}
pub fn ffi(message: impl Into<String>) -> Self {
MgError::Ffi(message.into())
}
#[deprecated(note = "Use specific error constructors instead")]
pub fn new(message: String) -> Self {
MgError::Connection(message)
}
}