use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("IO error: {0}")]
Io(io::Error),
#[error("Certificate error: {0}")]
CertificateError(String),
#[error("TLS error: {0}")]
TlsError(String),
#[error("HTTP error: {0}")]
HttpError(http::Error),
#[error("Slinger error: {0}")]
SlingerError(slinger::Error),
#[error("Proxy error: {0}")]
ProxyError(String),
#[error("Invalid request: {0}")]
InvalidRequest(String),
#[error("Connection error: {0}")]
ConnectionError(String),
#[error("{0}")]
Other(String),
}
impl Error {
pub fn certificate_error(msg: impl Into<String>) -> Self {
let error = Error::CertificateError(msg.into());
tracing::error!("Certificate error: {}", error);
error
}
pub fn tls_error(msg: impl Into<String>) -> Self {
let error = Error::TlsError(msg.into());
tracing::error!("TLS error: {}", error);
error
}
pub fn proxy_error(msg: impl Into<String>) -> Self {
let error = Error::ProxyError(msg.into());
tracing::error!("Proxy error: {}", error);
error
}
pub fn invalid_request(msg: impl Into<String>) -> Self {
let error = Error::InvalidRequest(msg.into());
tracing::error!("Invalid request: {}", error);
error
}
pub fn connection_error(msg: impl Into<String>) -> Self {
let error = Error::ConnectionError(msg.into());
tracing::error!("Connection error: {}", error);
error
}
pub fn other(msg: impl Into<String>) -> Self {
let error = Error::Other(msg.into());
tracing::error!("Other error: {}", error);
error
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
let error = Error::Io(value);
tracing::error!("IO error: {}", error);
error
}
}
impl From<http::Error> for Error {
fn from(value: http::Error) -> Self {
let error = Error::HttpError(value);
tracing::error!("HTTP error: {}", error);
error
}
}
impl From<slinger::Error> for Error {
fn from(value: slinger::Error) -> Self {
let error = Error::SlingerError(value);
tracing::error!("Slinger error: {}", error);
error
}
}