From cc584f07e03be1cf1449b0b5d0b59cd630a09533 Mon Sep 17 00:00:00 2001 From: Zhang Jingqiang Date: Thu, 21 Mar 2024 17:57:12 +0800 Subject: [PATCH] use less String in error types --- g3proxy/src/escape/direct_fixed/mod.rs | 6 ++--- g3proxy/src/escape/direct_float/mod.rs | 6 ++--- g3proxy/src/escape/route_geoip/mod.rs | 2 +- g3proxy/src/escape/route_resolved/mod.rs | 2 +- g3proxy/src/resolve/handle.rs | 4 +--- lib/g3-icap-client/src/reqmod/error.rs | 4 ++-- lib/g3-icap-client/src/reqmod/payload.rs | 28 ++++++++--------------- lib/g3-icap-client/src/respmod/error.rs | 4 ++-- lib/g3-icap-client/src/respmod/payload.rs | 16 +++++-------- lib/g3-resolver/src/error.rs | 2 +- 10 files changed, 30 insertions(+), 44 deletions(-) diff --git a/g3proxy/src/escape/direct_fixed/mod.rs b/g3proxy/src/escape/direct_fixed/mod.rs index 63e0f2d3..f02cba0d 100644 --- a/g3proxy/src/escape/direct_fixed/mod.rs +++ b/g3proxy/src/escape/direct_fixed/mod.rs @@ -191,9 +191,9 @@ impl DirectFixedEscaper { let ips = resolver_job .get_r1_or_first(self.config.happy_eyeballs.resolution_delay(), usize::MAX) .await?; - strategy.pick_best(ips).ok_or_else(|| { - ResolveError::UnexpectedError("no upstream ip can be selected".to_string()) - }) + strategy + .pick_best(ips) + .ok_or_else(|| ResolveError::UnexpectedError("no upstream ip can be selected")) } async fn redirect_get_best( diff --git a/g3proxy/src/escape/direct_float/mod.rs b/g3proxy/src/escape/direct_float/mod.rs index 62de96e7..2493f387 100644 --- a/g3proxy/src/escape/direct_float/mod.rs +++ b/g3proxy/src/escape/direct_float/mod.rs @@ -359,9 +359,9 @@ impl DirectFloatEscaper { let ips = resolver_job .get_r1_or_first(self.config.happy_eyeballs.resolution_delay(), usize::MAX) .await?; - strategy.pick_best(ips).ok_or_else(|| { - ResolveError::UnexpectedError("no upstream ip can be selected".to_string()) - }) + strategy + .pick_best(ips) + .ok_or_else(|| ResolveError::UnexpectedError("no upstream ip can be selected")) } async fn redirect_get_best( diff --git a/g3proxy/src/escape/route_geoip/mod.rs b/g3proxy/src/escape/route_geoip/mod.rs index 331222ee..7b8df0df 100644 --- a/g3proxy/src/escape/route_geoip/mod.rs +++ b/g3proxy/src/escape/route_geoip/mod.rs @@ -172,7 +172,7 @@ impl RouteGeoIpEscaper { .await?; self.config.resolve_strategy.pick_best(v).ok_or_else(|| { ResolveError::UnexpectedError( - "resolver job return ok but with no ip can be selected".to_string(), + "resolver job return ok but with no ip can be selected", ) }) } diff --git a/g3proxy/src/escape/route_resolved/mod.rs b/g3proxy/src/escape/route_resolved/mod.rs index eb1b0a99..590d5e06 100644 --- a/g3proxy/src/escape/route_resolved/mod.rs +++ b/g3proxy/src/escape/route_resolved/mod.rs @@ -126,7 +126,7 @@ impl RouteResolvedEscaper { .await?; self.config.resolve_strategy.pick_best(v).ok_or_else(|| { ResolveError::UnexpectedError( - "resolver job return ok but with no ip can be selected".to_string(), + "resolver job return ok but with no ip can be selected", ) }) } diff --git a/g3proxy/src/resolve/handle.rs b/g3proxy/src/resolve/handle.rs index dc80aaa6..11f0750e 100644 --- a/g3proxy/src/resolve/handle.rs +++ b/g3proxy/src/resolve/handle.rs @@ -446,9 +446,7 @@ impl ArriveFirstResolveJob { ) -> Poll> { let ips = ready!(self.poll_all_addrs(cx))?; let ip = self.strategy.pick_best(ips).ok_or_else(|| { - ResolveError::UnexpectedError( - "resolver job return ok but with no ip can be selected".to_string(), - ) + ResolveError::UnexpectedError("resolver job return ok but with no ip can be selected") })?; Poll::Ready(Ok(ip)) } diff --git a/lib/g3-icap-client/src/reqmod/error.rs b/lib/g3-icap-client/src/reqmod/error.rs index 0702a83a..bdc716fe 100644 --- a/lib/g3-icap-client/src/reqmod/error.rs +++ b/lib/g3-icap-client/src/reqmod/error.rs @@ -34,8 +34,8 @@ pub enum IcapReqmodParseError { InvalidHeaderLine(IcapLineParseError), #[error("no ISTag set")] NoServiceTagSet, - #[error("unsupported body")] - UnsupportedBody(String), + #[error("unsupported body: {0}")] + UnsupportedBody(&'static str), #[error("invalid value for header {0}")] InvalidHeaderValue(&'static str), #[error("io failed: {0:?}")] diff --git a/lib/g3-icap-client/src/reqmod/payload.rs b/lib/g3-icap-client/src/reqmod/payload.rs index bd59ce6f..ccd5e20c 100644 --- a/lib/g3-icap-client/src/reqmod/payload.rs +++ b/lib/g3-icap-client/src/reqmod/payload.rs @@ -40,7 +40,7 @@ impl IcapReqmodResponsePayload { .ok_or(IcapReqmodParseError::InvalidHeaderValue("Encapsulated"))?; if value.ne("0") { return Err(IcapReqmodParseError::UnsupportedBody( - "invalid hdr byte-offsets value".to_string(), + "invalid hdr byte-offsets value", )); } @@ -49,27 +49,23 @@ impl IcapReqmodResponsePayload { let body_part = parts .next() .ok_or_else(|| { - IcapReqmodParseError::UnsupportedBody( - "no body byte-offsets pair found".to_string(), - ) + IcapReqmodParseError::UnsupportedBody("no body byte-offsets pair found") })? .trim(); let (name, value) = body_part.split_once('=').ok_or_else(|| { - IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets pair".to_string(), - ) + IcapReqmodParseError::UnsupportedBody("invalid body byte-offsets pair") })?; let (hdr_len, offset) = usize::from_radix_10(value.as_bytes()); if offset != value.len() { return Err(IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets value".to_string(), + "invalid body byte-offsets value", )); } match name.to_lowercase().as_str() { "req-body" => Ok(IcapReqmodResponsePayload::HttpRequestWithBody(hdr_len)), "null-body" => Ok(IcapReqmodResponsePayload::HttpRequestWithoutBody(hdr_len)), _ => Err(IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets name".to_string(), + "invalid body byte-offsets name", )), } } @@ -77,33 +73,29 @@ impl IcapReqmodResponsePayload { let body_part = parts .next() .ok_or_else(|| { - IcapReqmodParseError::UnsupportedBody( - "no body byte-offsets pair found".to_string(), - ) + IcapReqmodParseError::UnsupportedBody("no body byte-offsets pair found") })? .trim(); let (name, value) = body_part.split_once('=').ok_or_else(|| { - IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets pair".to_string(), - ) + IcapReqmodParseError::UnsupportedBody("invalid body byte-offsets pair") })?; let (hdr_len, offset) = usize::from_radix_10(value.as_bytes()); if offset != value.len() { return Err(IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets value".to_string(), + "invalid body byte-offsets value", )); } match name.to_lowercase().as_str() { "res-body" => Ok(IcapReqmodResponsePayload::HttpResponseWithBody(hdr_len)), "null-body" => Ok(IcapReqmodResponsePayload::HttpResponseWithoutBody(hdr_len)), _ => Err(IcapReqmodParseError::UnsupportedBody( - "invalid body byte-offsets name".to_string(), + "invalid body byte-offsets name", )), } } "null-body" => Ok(IcapReqmodResponsePayload::NoPayload), _ => Err(IcapReqmodParseError::UnsupportedBody( - "invalid hdr byte-offsets value".to_string(), + "invalid hdr byte-offsets value", )), } } diff --git a/lib/g3-icap-client/src/respmod/error.rs b/lib/g3-icap-client/src/respmod/error.rs index 151f25fd..df41d92d 100644 --- a/lib/g3-icap-client/src/respmod/error.rs +++ b/lib/g3-icap-client/src/respmod/error.rs @@ -34,8 +34,8 @@ pub enum IcapRespmodParseError { InvalidHeaderLine(IcapLineParseError), #[error("no ISTag set")] NoServiceTagSet, - #[error("unsupported body")] - UnsupportedBody(String), + #[error("unsupported body: {0}")] + UnsupportedBody(&'static str), #[error("invalid value for header {0}")] InvalidHeaderValue(&'static str), #[error("io failed: {0:?}")] diff --git a/lib/g3-icap-client/src/respmod/payload.rs b/lib/g3-icap-client/src/respmod/payload.rs index 76b67c6b..126e8d9c 100644 --- a/lib/g3-icap-client/src/respmod/payload.rs +++ b/lib/g3-icap-client/src/respmod/payload.rs @@ -38,7 +38,7 @@ impl IcapRespmodResponsePayload { .ok_or(IcapRespmodParseError::InvalidHeaderValue("Encapsulated"))?; if value.ne("0") { return Err(IcapRespmodParseError::UnsupportedBody( - "invalid hdr byte-offsets value".to_string(), + "invalid hdr byte-offsets value", )); } @@ -47,33 +47,29 @@ impl IcapRespmodResponsePayload { let body_part = parts .next() .ok_or_else(|| { - IcapRespmodParseError::UnsupportedBody( - "no body byte-offsets pair found".to_string(), - ) + IcapRespmodParseError::UnsupportedBody("no body byte-offsets pair found") })? .trim(); let (name, value) = body_part.split_once('=').ok_or_else(|| { - IcapRespmodParseError::UnsupportedBody( - "invalid body byte-offsets pair".to_string(), - ) + IcapRespmodParseError::UnsupportedBody("invalid body byte-offsets pair") })?; let (hdr_len, offset) = usize::from_radix_10(value.as_bytes()); if offset != value.len() { return Err(IcapRespmodParseError::UnsupportedBody( - "invalid body byte-offsets value".to_string(), + "invalid body byte-offsets value", )); } match name.to_lowercase().as_str() { "res-body" => Ok(IcapRespmodResponsePayload::HttpResponseWithBody(hdr_len)), "null-body" => Ok(IcapRespmodResponsePayload::HttpResponseWithoutBody(hdr_len)), _ => Err(IcapRespmodParseError::UnsupportedBody( - "invalid body byte-offsets name".to_string(), + "invalid body byte-offsets name", )), } } "null-body" => Ok(IcapRespmodResponsePayload::NoPayload), _ => Err(IcapRespmodParseError::UnsupportedBody( - "invalid hdr byte-offsets value".to_string(), + "invalid hdr byte-offsets value", )), } } diff --git a/lib/g3-resolver/src/error.rs b/lib/g3-resolver/src/error.rs index 9698b7d3..7588efd2 100644 --- a/lib/g3-resolver/src/error.rs +++ b/lib/g3-resolver/src/error.rs @@ -105,7 +105,7 @@ pub enum ResolveError { #[error("local error: {0}")] FromLocal(#[from] ResolveLocalError), #[error("unexpected error: {0}")] - UnexpectedError(String), + UnexpectedError(&'static str), } impl ResolveError {