slinger 0.2.14

An HTTP Client for Rust designed for hackers.
Documentation
//! engine error
use std::io::ErrorKind;
use std::num::ParseIntError;
use thiserror::Error as ThisError;
/// A `Result` alias where the `Err` case is `slinger::Error`.
pub type Result<T> = std::result::Result<T, Error>;
/// The Errors that may occur when processing a `slinger`.
#[derive(ThisError, Debug)]
pub enum Error {
  #[error(transparent)]
  #[cfg(feature = "rustls")]
  /// tls Error
  Tls(#[from] tokio_rustls::rustls::Error),
  /// Error
  #[error(transparent)]
  IO(#[from] std::io::Error),
  /// http::Error
  #[error(transparent)]
  Http(http::Error),
  /// ParseIntError
  #[error(transparent)]
  IntError(#[from] ParseIntError),
  /// Proxy ReplyError
  #[error(transparent)]
  ReplyError(#[from] ReplyError),
  /// Unknown Error
  #[error("other")]
  Other(String),
}

#[derive(ThisError, Debug)]
pub enum ReplyError {
  #[error("Succeeded")]
  Succeeded,
  #[error("General failure")]
  GeneralFailure,
  #[error("Connection not allowed by ruleset")]
  ConnectionNotAllowed,
  #[error("Network unreachable")]
  NetworkUnreachable,
  #[error("Host unreachable")]
  HostUnreachable,
  #[error("Connection refused")]
  ConnectionRefused,
  #[error("TTL expired")]
  TtlExpired,
  #[error("Command not supported")]
  CommandNotSupported,
  #[error("Address type not supported")]
  AddressTypeNotSupported,
  //    OtherReply(u8),
}

impl From<http::Error> for Error {
  fn from(value: http::Error) -> Self {
    let error = Error::Http(value);
    tracing::error!("HTTP error: {}", error);
    error
  }
}

impl From<http::header::InvalidHeaderValue> for Error {
  fn from(value: http::header::InvalidHeaderValue) -> Self {
    let error = Error::Http(http::Error::from(value));
    tracing::error!("Invalid header value error: {}", error);
    error
  }
}

pub(crate) fn new_io_error(error_kind: ErrorKind, msg: &str) -> Error {
  let error = Error::IO(std::io::Error::new(error_kind, msg));
  tracing::error!("IO error: {}", error);
  error
}

impl Error {
  pub(crate) fn invalid_request(msg: impl Into<String>) -> Error {
    let error = Error::Other(msg.into());
    tracing::error!("Invalid request: {}", error);
    error
  }
}
#[cfg(feature = "tls")]
pub(crate) fn builder<E: Into<Box<dyn std::error::Error + Send + Sync>>>(e: E) -> Error {
  let error = Error::Other(e.into().to_string());
  tracing::error!("Builder error: {}", error);
  error
}