From f846d4021ce2a76f2b62c6f82eb65faee892931d Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Wed, 14 Feb 2024 23:35:58 +1100 Subject: [PATCH] Return useful error when encountering unknown kafka API key (#1477) --- shotover/src/frame/kafka.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/shotover/src/frame/kafka.rs b/shotover/src/frame/kafka.rs index 185685f68..7f77421f5 100644 --- a/shotover/src/frame/kafka.rs +++ b/shotover/src/frame/kafka.rs @@ -139,14 +139,13 @@ impl KafkaFrame { fn parse_request(mut bytes: Bytes) -> Result { let api_key = i16::from_be_bytes(bytes[0..2].try_into().unwrap()); let api_version = i16::from_be_bytes(bytes[2..4].try_into().unwrap()); - let header_version = ApiKey::try_from(api_key) - .unwrap() - .request_header_version(api_version); + let api_key = + ApiKey::try_from(api_key).map_err(|_| anyhow!("unknown api key {api_key}"))?; + + let header_version = api_key.request_header_version(api_version); let header = RequestHeader::decode(&mut bytes, header_version) .context("Failed to decode request header")?; - let api_key = ApiKey::try_from(header.request_api_key) - .map_err(|_| anyhow!("unknown api key {}", header.request_api_key))?; let version = header.request_api_version; let body = match api_key { ApiKey::ProduceKey => RequestBody::Produce(decode(&mut bytes, version)?),