Skip to main content

slinger_mitm/
error.rs

1//! Error types for MITM proxy
2
3use std::io;
4use thiserror::Error;
5
6/// Result type for MITM operations
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Error types for MITM proxy operations
10#[derive(Error, Debug)]
11pub enum Error {
12  /// IO error
13  #[error("IO error: {0}")]
14  Io(io::Error),
15
16  /// Certificate error
17  #[error("Certificate error: {0}")]
18  CertificateError(String),
19
20  /// TLS error
21  #[error("TLS error: {0}")]
22  TlsError(String),
23
24  /// HTTP parsing error
25  #[error("HTTP error: {0}")]
26  HttpError(http::Error),
27
28  /// Slinger client error
29  #[error("Slinger error: {0}")]
30  SlingerError(slinger::Error),
31
32  /// Proxy error
33  #[error("Proxy error: {0}")]
34  ProxyError(String),
35
36  /// Invalid request
37  #[error("Invalid request: {0}")]
38  InvalidRequest(String),
39
40  /// Connection error
41  #[error("Connection error: {0}")]
42  ConnectionError(String),
43
44  /// Other errors
45  #[error("{0}")]
46  Other(String),
47}
48
49impl Error {
50  /// Create a certificate error and log it
51  pub fn certificate_error(msg: impl Into<String>) -> Self {
52    let error = Error::CertificateError(msg.into());
53    tracing::error!("Certificate error: {}", error);
54    error
55  }
56
57  /// Create a TLS error and log it
58  pub fn tls_error(msg: impl Into<String>) -> Self {
59    let error = Error::TlsError(msg.into());
60    tracing::error!("TLS error: {}", error);
61    error
62  }
63
64  /// Create a proxy error and log it
65  pub fn proxy_error(msg: impl Into<String>) -> Self {
66    let error = Error::ProxyError(msg.into());
67    tracing::error!("Proxy error: {}", error);
68    error
69  }
70
71  /// Create an invalid request error and log it
72  pub fn invalid_request(msg: impl Into<String>) -> Self {
73    let error = Error::InvalidRequest(msg.into());
74    tracing::error!("Invalid request: {}", error);
75    error
76  }
77
78  /// Create a connection error and log it
79  pub fn connection_error(msg: impl Into<String>) -> Self {
80    let error = Error::ConnectionError(msg.into());
81    tracing::error!("Connection error: {}", error);
82    error
83  }
84
85  /// Create an other error and log it
86  pub fn other(msg: impl Into<String>) -> Self {
87    let error = Error::Other(msg.into());
88    tracing::error!("Other error: {}", error);
89    error
90  }
91}
92
93impl From<io::Error> for Error {
94  fn from(value: io::Error) -> Self {
95    let error = Error::Io(value);
96    tracing::error!("IO error: {}", error);
97    error
98  }
99}
100
101impl From<http::Error> for Error {
102  fn from(value: http::Error) -> Self {
103    let error = Error::HttpError(value);
104    tracing::error!("HTTP error: {}", error);
105    error
106  }
107}
108
109impl From<slinger::Error> for Error {
110  fn from(value: slinger::Error) -> Self {
111    let error = Error::SlingerError(value);
112    tracing::error!("Slinger error: {}", error);
113    error
114  }
115}