Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling on encode version violation #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions protocol_codegen/src/generate_messages/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ fn write_encode_field<W: Write>(
// field is used in this version, encode it
|w| write_encode_field_inner(w, field, valid_versions, compute_size),
// field is not present in this version, ensure that the default value is used
|w| write_default_check(w, field),
|w| write_fail_if_field_set(w, field),
false,
field.ignorable,
)
Expand Down Expand Up @@ -457,19 +457,22 @@ fn write_encode_field_inner<W: Write>(
}
}

fn write_default_check<W: Write>(
fn write_fail_if_field_set<W: Write>(
w: &mut CodeWriter<W>,
field: &PreparedField,
) -> Result<(), Error> {
let var_name = field.var_name();

let is_default = field
let is_not_default = field
.default
.gen_is_default(&var_name, field.optional)
.not();
write!(w, "if {is_default} ")?;
write!(w, "if {is_not_default} ")?;
w.block(|w| {
write!(w, r#"bail!("failed to encode");"#)?;
write!(
w,
r#"bail!("A field is set that is not available on the selected protocol version");"#
)?;
Ok(())
})
}
Expand Down Expand Up @@ -596,7 +599,7 @@ fn write_encode_tag_buffer<W: Write>(
compute_size,
)
},
|w| write_default_check(w, field),
|w| write_fail_if_field_set(w, field),
false,
field.ignorable,
)?;
Expand Down
40 changes: 20 additions & 20 deletions src/messages/add_partitions_to_txn_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Encodable for AddPartitionsToTxnRequest {
types::CompactArray(types::Struct { version }).encode(buf, &self.transactions)?;
} else {
if !self.transactions.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -126,21 +126,21 @@ impl Encodable for AddPartitionsToTxnRequest {
}
} else {
if !self.v3_and_below_transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
types::Int64.encode(buf, &self.v3_and_below_producer_id)?;
} else {
if self.v3_and_below_producer_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
types::Int16.encode(buf, &self.v3_and_below_producer_epoch)?;
} else {
if self.v3_and_below_producer_epoch != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -152,7 +152,7 @@ impl Encodable for AddPartitionsToTxnRequest {
}
} else {
if !self.v3_and_below_topics.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand All @@ -176,7 +176,7 @@ impl Encodable for AddPartitionsToTxnRequest {
types::CompactArray(types::Struct { version }).compute_size(&self.transactions)?;
} else {
if !self.transactions.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -188,21 +188,21 @@ impl Encodable for AddPartitionsToTxnRequest {
}
} else {
if !self.v3_and_below_transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
total_size += types::Int64.compute_size(&self.v3_and_below_producer_id)?;
} else {
if self.v3_and_below_producer_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
total_size += types::Int16.compute_size(&self.v3_and_below_producer_epoch)?;
} else {
if self.v3_and_below_producer_epoch != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -215,7 +215,7 @@ impl Encodable for AddPartitionsToTxnRequest {
}
} else {
if !self.v3_and_below_topics.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand Down Expand Up @@ -557,35 +557,35 @@ impl Encodable for AddPartitionsToTxnTransaction {
types::CompactString.encode(buf, &self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::Int64.encode(buf, &self.producer_id)?;
} else {
if self.producer_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::Int16.encode(buf, &self.producer_epoch)?;
} else {
if self.producer_epoch != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::Boolean.encode(buf, &self.verify_only)?;
} else {
if self.verify_only {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
} else {
if !self.topics.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand All @@ -608,36 +608,36 @@ impl Encodable for AddPartitionsToTxnTransaction {
total_size += types::CompactString.compute_size(&self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size += types::Int64.compute_size(&self.producer_id)?;
} else {
if self.producer_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size += types::Int16.compute_size(&self.producer_epoch)?;
} else {
if self.producer_epoch != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size += types::Boolean.compute_size(&self.verify_only)?;
} else {
if self.verify_only {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
} else {
if !self.topics.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand Down
16 changes: 8 additions & 8 deletions src/messages/add_partitions_to_txn_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Encodable for AddPartitionsToTxnResponse {
.encode(buf, &self.results_by_transaction)?;
} else {
if !self.results_by_transaction.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -248,7 +248,7 @@ impl Encodable for AddPartitionsToTxnResponse {
}
} else {
if !self.results_by_topic_v3_and_below.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand Down Expand Up @@ -276,7 +276,7 @@ impl Encodable for AddPartitionsToTxnResponse {
.compute_size(&self.results_by_transaction)?;
} else {
if !self.results_by_transaction.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version <= 3 {
Expand All @@ -289,7 +289,7 @@ impl Encodable for AddPartitionsToTxnResponse {
}
} else {
if !self.results_by_topic_v3_and_below.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand Down Expand Up @@ -424,14 +424,14 @@ impl Encodable for AddPartitionsToTxnResult {
types::CompactString.encode(buf, &self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
types::CompactArray(types::Struct { version }).encode(buf, &self.topic_results)?;
} else {
if !self.topic_results.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand All @@ -454,15 +454,15 @@ impl Encodable for AddPartitionsToTxnResult {
total_size += types::CompactString.compute_size(&self.transactional_id)?;
} else {
if !self.transactional_id.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 4 {
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.topic_results)?;
} else {
if !self.topic_results.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
Expand Down
20 changes: 10 additions & 10 deletions src/messages/alter_partition_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ impl Encodable for BrokerState {
types::Int32.encode(buf, &self.broker_id)?;
} else {
if self.broker_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
types::Int64.encode(buf, &self.broker_epoch)?;
} else {
if self.broker_epoch != -1 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
let num_tagged_fields = self.unknown_tagged_fields.len();
Expand All @@ -240,14 +240,14 @@ impl Encodable for BrokerState {
total_size += types::Int32.compute_size(&self.broker_id)?;
} else {
if self.broker_id != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
total_size += types::Int64.compute_size(&self.broker_epoch)?;
} else {
if self.broker_epoch != -1 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
let num_tagged_fields = self.unknown_tagged_fields.len();
Expand Down Expand Up @@ -422,22 +422,22 @@ impl Encodable for PartitionData {
types::CompactArray(types::Int32).encode(buf, &self.new_isr)?;
} else {
if !self.new_isr.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
types::CompactArray(types::Struct { version })
.encode(buf, &self.new_isr_with_epochs)?;
} else {
if !self.new_isr_with_epochs.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
types::Int8.encode(buf, &self.leader_recovery_state)?;
} else {
if self.leader_recovery_state != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
types::Int32.encode(buf, &self.partition_epoch)?;
Expand All @@ -461,22 +461,22 @@ impl Encodable for PartitionData {
total_size += types::CompactArray(types::Int32).compute_size(&self.new_isr)?;
} else {
if !self.new_isr.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 3 {
total_size += types::CompactArray(types::Struct { version })
.compute_size(&self.new_isr_with_epochs)?;
} else {
if !self.new_isr_with_epochs.is_empty() {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 1 {
total_size += types::Int8.compute_size(&self.leader_recovery_state)?;
} else {
if self.leader_recovery_state != 0 {
bail!("failed to encode");
bail!("A field is set that is not available on the selected protocol version");
}
}
total_size += types::Int32.compute_size(&self.partition_epoch)?;
Expand Down
Loading