From 06d17408b57ac635f0552ac18df764297b60a946 Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 17:55:59 -0800 Subject: [PATCH 1/6] remove custom errors --- src/addresses.rs | 2 +- src/config.rs | 4 ++-- src/errors.rs | 6 ++++-- src/main.rs | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/addresses.rs b/src/addresses.rs index 6086b3f..46bddee 100644 --- a/src/addresses.rs +++ b/src/addresses.rs @@ -33,7 +33,7 @@ fn add_addresses_to_map( let incoming_host = match incoming_uri.host() { Some(h) => h, _ => { - return Err(Error::Custom( + return Err(Error::Addresses( "could not parse host from source uri".to_string(), )); } diff --git a/src/config.rs b/src/config.rs index cdd4c4c..285106f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,8 +34,8 @@ pub async fn from_filepath(filepath: &PathBuf) -> Result { let parent_dir = match config_path.parent() { Some(p) => p.to_path_buf(), _ => { - return Err(Error::Custom( - "parent directory of config not found".to_string(), + return Err(Error::Config( + "Parent directory of config not found".to_string(), )); } }; diff --git a/src/errors.rs b/src/errors.rs index 6fd83e2..4d31028 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,21 +6,23 @@ use std::io::Error as IoError; #[derive(Debug)] pub enum Error { + Addresses(String), + Config(String), Io(IoError), SerdeJson(SerdeJsonError), NativeTls(NativeTlsError), Uri(InvalidUri), - Custom(String), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + Error::Addresses(err) => write!(f, "{}", err), + Error::Config(err) => write!(f, "{}", err), Error::Io(err) => write!(f, "{}", err), Error::NativeTls(err) => write!(f, "{}", err), Error::SerdeJson(err) => write!(f, "{}", err), Error::Uri(err) => write!(f, "{}", err), - Error::Custom(err) => write!(f, "{}", err), } } } diff --git a/src/main.rs b/src/main.rs index efbcfed..a15e6c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ async fn main() -> Result<(), Error> { let args = match env::args().nth(1) { Some(a) => path::PathBuf::from(a), None => { - return Err(Error::Custom( + return Err(Error::Config( "argument error: argv[0] config path not provided".to_string(), )); } From 6af90b62035333f36c3f874bb0c042daef28976f Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 17:57:37 -0800 Subject: [PATCH 2/6] env arg error message update --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a15e6c5..742a62f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ async fn main() -> Result<(), Error> { Some(a) => path::PathBuf::from(a), None => { return Err(Error::Config( - "argument error: argv[0] config path not provided".to_string(), + "Config path not provided at argv[0]".to_string(), )); } }; From 26b558e614498716d2c4c407bfa80be0bab6e960 Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 18:01:18 -0800 Subject: [PATCH 3/6] add error preface to output --- src/addresses.rs | 2 +- src/errors.rs | 12 ++++++------ src/requests.rs | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/addresses.rs b/src/addresses.rs index 46bddee..6365a32 100644 --- a/src/addresses.rs +++ b/src/addresses.rs @@ -34,7 +34,7 @@ fn add_addresses_to_map( Some(h) => h, _ => { return Err(Error::Addresses( - "could not parse host from source uri".to_string(), + "Could not parse host from source uri".to_string(), )); } }; diff --git a/src/errors.rs b/src/errors.rs index 4d31028..5d603c6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -17,12 +17,12 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::Addresses(err) => write!(f, "{}", err), - Error::Config(err) => write!(f, "{}", err), - Error::Io(err) => write!(f, "{}", err), - Error::NativeTls(err) => write!(f, "{}", err), - Error::SerdeJson(err) => write!(f, "{}", err), - Error::Uri(err) => write!(f, "{}", err), + Error::Addresses(err) => write!(f, "[addresses] {}", err), + Error::Config(err) => write!(f, "[config] {}", err), + Error::Io(err) => write!(f, "[io] {}", err), + Error::NativeTls(err) => write!(f, "[native_tls] {}", err), + Error::SerdeJson(err) => write!(f, "[serde_json] {}", err), + Error::Uri(err) => write!(f, "[uri] {}", err), } } } diff --git a/src/requests.rs b/src/requests.rs index 656ac5b..e5234dd 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -16,7 +16,7 @@ const FAILED_TO_PROCESS_REQUEST_ERROR: &str = "failed to process request"; fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> { let host = match uri.host() { Some(h) => h.to_string(), - _ => return Err("failed to retrieve downstream URI from upstream URI"), + _ => return Err("Failed to retrieve downstream URI from upstream URI"), }; let port = match uri.port() { @@ -40,7 +40,7 @@ fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> { async fn create_tcp_stream<'a>(addr: &str) -> Result, &'a str> { match TcpStream::connect(&addr).await { Ok(client_stream) => Ok(TokioIo::new(client_stream)), - _ => Err("failed to establish tcp connection"), + _ => Err("Failed to establish tcp connection"), } } @@ -54,18 +54,18 @@ async fn create_tls_stream<'a>( let cx = match builder.build() { Ok(c) => c, - _ => return Err("failed to build TLS connection"), + _ => return Err("Failed to build TLS connection"), }; let tls_connector = tokio_native_tls::TlsConnector::from(cx); let client_stream = match TcpStream::connect(addr).await { Ok(s) => s, - _ => return Err("failed to establish TCP connection"), + _ => return Err("Failed to establish TCP connection"), }; let tls_stream = match tls_connector.connect(host, client_stream).await { Ok(s) => TokioIo::new(s), - _ => return Err("failed to establish TLS connection"), + _ => return Err("TLS connection failed"), }; Ok(tls_stream) From f6141be2e3ac29aa9b1dafb22309ce8c7bd6cc8d Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 18:10:48 -0800 Subject: [PATCH 4/6] reference struct in CLI output --- src/errors.rs | 12 ++++++------ src/requests.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 5d603c6..7ee964c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -17,12 +17,12 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Error::Addresses(err) => write!(f, "[addresses] {}", err), - Error::Config(err) => write!(f, "[config] {}", err), - Error::Io(err) => write!(f, "[io] {}", err), - Error::NativeTls(err) => write!(f, "[native_tls] {}", err), - Error::SerdeJson(err) => write!(f, "[serde_json] {}", err), - Error::Uri(err) => write!(f, "[uri] {}", err), + Error::Addresses(err) => write!(f, "[Error::Addresses] {}", err), + Error::Config(err) => write!(f, "[Error::Config] {}", err), + Error::Io(err) => write!(f, "[Error::Io] {}", err), + Error::NativeTls(err) => write!(f, "[Error::Native_tls] {}", err), + Error::SerdeJson(err) => write!(f, "[Error::Serde_json] {}", err), + Error::Uri(err) => write!(f, "[Error::Uri] {}", err), } } } diff --git a/src/requests.rs b/src/requests.rs index e5234dd..f2a3e9c 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -16,7 +16,7 @@ const FAILED_TO_PROCESS_REQUEST_ERROR: &str = "failed to process request"; fn get_host_and_authority<'a>(uri: &Uri) -> Result<(String, String), &'a str> { let host = match uri.host() { Some(h) => h.to_string(), - _ => return Err("Failed to retrieve downstream URI from upstream URI"), + _ => return Err("Failed to foward incoming URI to outgoing URI"), }; let port = match uri.port() { From 3497230dfb4d9e0abcf743920d8d454deb513dfb Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 18:16:57 -0800 Subject: [PATCH 5/6] canonicalize config path --- src/config.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 285106f..63ecf07 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,7 @@ use crate::errors::Error; use serde::{Deserialize, Serialize}; use serde_json; -use std::path; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tokio::fs; #[derive(Clone, Serialize, Deserialize, Debug)] @@ -20,8 +19,8 @@ pub struct Config { pub dangerous_self_signed_addresses: Option>, } -pub async fn from_filepath(filepath: &PathBuf) -> Result { - let config_path = match path::absolute(filepath) { +pub async fn from_filepath(filepath: &Path) -> Result { + let config_path = match filepath.canonicalize() { Ok(pb) => pb, Err(e) => return Err(Error::Io(e)), }; From f605b744ef564821591ea553f629c1e719fd34a4 Mon Sep 17 00:00:00 2001 From: Taylor Vann Date: Tue, 10 Nov 2026 18:28:46 -0800 Subject: [PATCH 6/6] constants after types --- src/response.rs | 7 +++---- src/service.rs | 7 ++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/response.rs b/src/response.rs index d744201..67133a5 100644 --- a/src/response.rs +++ b/src/response.rs @@ -6,10 +6,6 @@ use hyper::http::uri::InvalidUriParts; use hyper::{Request, Response, StatusCode, Uri}; use std::collections::HashMap; -pub type BoxedResponse = Response>; - -pub const URI_FROM_REQUEST_ERROR: &str = "unable to parse upstream URI from request"; -pub const UPSTREAM_URI_ERROR: &str = "falied to update request with upstream URI"; #[derive(Clone, Debug)] pub struct AddressParams { @@ -18,6 +14,9 @@ pub struct AddressParams { } pub type AddressMap = HashMap; +pub type BoxedResponse = Response>; + +pub const UPSTREAM_URI_ERROR: &str = "Falied to update request with upstream URI"; fn update_request_with_dest_uri( mut req: Request, diff --git a/src/service.rs b/src/service.rs index c94353d..6c71dd7 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,5 +1,4 @@ use crate::requests; -use crate::response; use crate::response::{AddressMap, BoxedResponse, build_response}; use hyper::body::Incoming; use hyper::http; @@ -9,6 +8,8 @@ use std::future::Future; use std::pin::Pin; use std::sync::Arc; +pub const URI_FROM_REQUEST_ERROR: &str = "Unnable to parse upstream URI from request"; + pub struct Svc { pub addresses: Arc, } @@ -27,7 +28,7 @@ impl Service> for Svc { return Box::pin(async move { requests::create_fallback_response( &StatusCode::BAD_REQUEST, - &response::URI_FROM_REQUEST_ERROR, + URI_FROM_REQUEST_ERROR, ) }); } @@ -40,7 +41,7 @@ impl Service> for Svc { return Box::pin(async move { requests::create_fallback_response( &StatusCode::BAD_GATEWAY, - &response::URI_FROM_REQUEST_ERROR, + URI_FROM_REQUEST_ERROR, ) }); }