From 726f66a0364bd32409788589346eb02eb24a5a6c Mon Sep 17 00:00:00 2001 From: Adam Cattermole Date: Thu, 14 Nov 2024 14:01:05 +0000 Subject: [PATCH] Replace all unwraps Signed-off-by: Adam Cattermole --- src/configuration.rs | 11 ++++++----- src/data/attribute.rs | 5 ++++- src/data/cel.rs | 27 ++++++++++++++++++++------- src/filter.rs | 3 ++- src/service.rs | 12 ++++++++---- src/service/auth.rs | 6 +++--- src/service/rate_limit.rs | 2 +- 7 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index 9d49cc69..2e329e40 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -106,10 +106,7 @@ impl TryFrom for FilterConfig { .expect("Predicates must not be compiled yet!"); for datum in &action.data { - let result = datum.item.compile(); - if result.is_err() { - return Err(result.err().unwrap()); - } + datum.item.compile()?; } } @@ -204,7 +201,11 @@ impl<'de> Visitor<'de> for TimeoutVisitor { E: Error, { match duration(Arc::new(string)) { - Ok(Value::Duration(duration)) => Ok(Timeout(duration.to_std().unwrap())), + Ok(Value::Duration(duration)) => Ok(Timeout( + duration + .to_std() + .expect("duration should not be less than zero"), + )), Err(e) => Err(E::custom(e)), _ => Err(E::custom("Unsupported Duration Value")), } diff --git a/src/data/attribute.rs b/src/data/attribute.rs index 9e65caa9..e6336cb7 100644 --- a/src/data/attribute.rs +++ b/src/data/attribute.rs @@ -166,7 +166,10 @@ fn process_metadata(s: &Struct, prefix: String) -> Vec<(String, String)> { let nested_struct = value.get_struct_value(); result.extend(process_metadata(nested_struct, current_prefix)); } else if let Some(v) = json { - result.push((current_prefix, serde_json::to_string(&v).unwrap())); + result.push(( + current_prefix, + serde_json::to_string(&v).expect("failed to serialize json Value"), + )); } } result diff --git a/src/data/cel.rs b/src/data/cel.rs index 844c726b..039bfa25 100644 --- a/src/data/cel.rs +++ b/src/data/cel.rs @@ -87,7 +87,7 @@ impl Expression { /// Decodes the query string and returns a Map where the key is the parameter's name and /// the value is either a [`Value::String`] or a [`Value::List`] if the parameter's name is repeated -/// and the second arg is set not set to `false`. +/// and the second arg is not set to `false`. /// see [`tests::decodes_query_string`] fn decode_query_string(This(s): This>, Arguments(args): Arguments) -> ResolveResult { let allow_repeats = if args.len() == 2 { @@ -102,8 +102,16 @@ fn decode_query_string(This(s): This>, Arguments(args): Arguments) - for part in s.split('&') { let mut kv = part.split('='); if let (Some(key), Some(value)) = (kv.next(), kv.next().or(Some(""))) { - let new_v: Value = decode(value).unwrap().into_owned().into(); - match map.entry(decode(key).unwrap().into_owned().into()) { + let new_v: Value = decode(value) + .expect("failed to decode query string value") + .into_owned() + .into(); + match map.entry( + decode(key) + .expect("failed to decode query string key") + .into_owned() + .into(), + ) { Entry::Occupied(mut e) => { if allow_repeats { if let Value::List(ref mut list) = e.get_mut() { @@ -118,7 +126,12 @@ fn decode_query_string(This(s): This>, Arguments(args): Arguments) - } } Entry::Vacant(e) => { - e.insert(decode(value).unwrap().into_owned().into()); + e.insert( + decode(value) + .expect("failed to decode query string value") + .into_owned() + .into(), + ); } } } @@ -296,11 +309,11 @@ fn json_to_cel(json: &str) -> Value { JsonValue::Bool(b) => b.into(), JsonValue::Number(n) => { if n.is_u64() { - n.as_u64().unwrap().into() + n.as_u64().expect("number must be u64").into() } else if n.is_i64() { - n.as_i64().unwrap().into() + n.as_i64().expect("number must be i64").into() } else { - n.as_f64().unwrap().into() + n.as_f64().expect("number must be f64").into() } } JsonValue::String(str) => str.into(), diff --git a/src/filter.rs b/src/filter.rs index ab359182..ed9c2418 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -26,7 +26,8 @@ extern "C" fn start() { proxy_wasm::set_log_level(LogLevel::Trace); std::panic::set_hook(Box::new(|panic_info| { - proxy_wasm::hostcalls::log(LogLevel::Critical, &panic_info.to_string()).unwrap(); + proxy_wasm::hostcalls::log(LogLevel::Critical, &panic_info.to_string()) + .expect("failed to log panic_info"); })); proxy_wasm::set_root_context(|context_id| -> Box { info!("#{} set_root_context", context_id); diff --git a/src/service.rs b/src/service.rs index 404671fa..958f6476 100644 --- a/src/service.rs +++ b/src/service.rs @@ -57,7 +57,8 @@ impl GrpcService { ) -> Result { let failure_mode = operation.get_failure_mode(); if let Some(res_body_bytes) = - hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, 0, resp_size).unwrap() + hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, 0, resp_size) + .expect("failed to get_buffer on GrpcReceiveBuffer") { match GrpcMessageResponse::new(operation.get_service_type(), &res_body_bytes) { Ok(res) => match operation.get_service_type() { @@ -85,9 +86,11 @@ impl GrpcService { match failure_mode { FailureMode::Deny => { hostcalls::send_http_response(500, vec![], Some(b"Internal Server Error.\n")) - .unwrap(); + .expect("failed to send_http_response 500"); + } + FailureMode::Allow => { + hostcalls::resume_http_request().expect("failed to resume_http_request") } - FailureMode::Allow => hostcalls::resume_http_request().unwrap(), } } } @@ -140,7 +143,8 @@ impl GrpcServiceHandler { message: GrpcMessageRequest, timeout: Duration, ) -> Result { - let msg = Message::write_to_bytes(&message).unwrap(); + let msg = Message::write_to_bytes(&message) + .expect("failed to write the GrpcMessageRequest to bytes"); let metadata = self .header_resolver .get(get_map_values_bytes_fn) diff --git a/src/service/auth.rs b/src/service/auth.rs index 33af0689..925c4762 100644 --- a/src/service/auth.rs +++ b/src/service/auth.rs @@ -64,7 +64,7 @@ impl AuthService { let mut request = AttributeContext_Request::default(); let mut http = AttributeContext_HttpRequest::default(); let headers: HashMap = hostcalls::get_map(MapType::HttpRequestHeaders) - .unwrap() + .expect("failed to retrieve HttpRequestHeaders from host") .into_iter() .collect(); @@ -151,7 +151,7 @@ impl AuthService { header.get_header().get_key(), header.get_header().get_value(), ) - .unwrap() + .expect("failed to add_map_value to HttpRequestHeaders") }); Ok(GrpcResult::default()) } @@ -170,7 +170,7 @@ impl AuthService { response_headers, Some(denied_response.get_body().as_ref()), ) - .unwrap(); + .expect("failed to send_http_response"); Err(status_code) } None => { diff --git a/src/service/rate_limit.rs b/src/service/rate_limit.rs index 4d8f2424..d2a692f3 100644 --- a/src/service/rate_limit.rs +++ b/src/service/rate_limit.rs @@ -57,7 +57,7 @@ impl RateLimitService { response_headers.push((header.get_key(), header.get_value())); } hostcalls::send_http_response(429, response_headers, Some(b"Too Many Requests\n")) - .unwrap(); + .expect("failed to send_http_response 429 while OVER_LIMIT"); Err(StatusCode::TooManyRequests) } GrpcMessageResponse::RateLimit(RateLimitResponse {