Skip to content

Commit

Permalink
Replace all unwraps
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Cattermole <[email protected]>
  • Loading branch information
adam-cattermole committed Nov 14, 2024
1 parent 02a265b commit 726f66a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
11 changes: 6 additions & 5 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ impl TryFrom<PluginConfiguration> 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()?;
}
}

Expand Down Expand Up @@ -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")),
}
Expand Down
5 changes: 4 additions & 1 deletion src/data/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 20 additions & 7 deletions src/data/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<String>>, Arguments(args): Arguments) -> ResolveResult {
let allow_repeats = if args.len() == 2 {
Expand All @@ -102,8 +102,16 @@ fn decode_query_string(This(s): This<Arc<String>>, 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() {
Expand All @@ -118,7 +126,12 @@ fn decode_query_string(This(s): This<Arc<String>>, 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(),
);
}
}
}
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn RootContext> {
info!("#{} set_root_context", context_id);
Expand Down
12 changes: 8 additions & 4 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ impl GrpcService {
) -> Result<GrpcResult, StatusCode> {
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() {
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -140,7 +143,8 @@ impl GrpcServiceHandler {
message: GrpcMessageRequest,
timeout: Duration,
) -> Result<u32, Status> {
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)
Expand Down
6 changes: 3 additions & 3 deletions src/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl AuthService {
let mut request = AttributeContext_Request::default();
let mut http = AttributeContext_HttpRequest::default();
let headers: HashMap<String, String> = hostcalls::get_map(MapType::HttpRequestHeaders)
.unwrap()
.expect("failed to retrieve HttpRequestHeaders from host")
.into_iter()
.collect();

Expand Down Expand Up @@ -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())
}
Expand All @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion src/service/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 726f66a

Please sign in to comment.