Skip to content

Commit 0bfe79c

Browse files
committed
Improve error handling on encode version violation
1 parent fbb543e commit 0bfe79c

File tree

54 files changed

+399
-396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+399
-396
lines changed

protocol_codegen/src/generate_messages/generate.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn write_encode_field<W: Write>(
401401
// field is used in this version, encode it
402402
|w| write_encode_field_inner(w, field, valid_versions, compute_size),
403403
// field is not present in this version, ensure that the default value is used
404-
|w| write_default_check(w, field),
404+
|w| write_check_field_unset(w, field),
405405
false,
406406
field.ignorable,
407407
)
@@ -457,19 +457,22 @@ fn write_encode_field_inner<W: Write>(
457457
}
458458
}
459459

460-
fn write_default_check<W: Write>(
460+
fn write_check_field_unset<W: Write>(
461461
w: &mut CodeWriter<W>,
462462
field: &PreparedField,
463463
) -> Result<(), Error> {
464464
let var_name = field.var_name();
465465

466-
let is_default = field
466+
let is_not_default = field
467467
.default
468468
.gen_is_default(&var_name, field.optional)
469469
.not();
470-
write!(w, "if {is_default} ")?;
470+
write!(w, "if {is_not_default} ")?;
471471
w.block(|w| {
472-
write!(w, r#"bail!("failed to encode");"#)?;
472+
write!(
473+
w,
474+
r#"bail!("A field is set that is not available on the selected protocol version");"#
475+
)?;
473476
Ok(())
474477
})
475478
}
@@ -596,7 +599,7 @@ fn write_encode_tag_buffer<W: Write>(
596599
compute_size,
597600
)
598601
},
599-
|w| write_default_check(w, field),
602+
|w| write_check_field_unset(w, field),
600603
false,
601604
field.ignorable,
602605
)?;

src/messages/add_partitions_to_txn_request.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Encodable for AddPartitionsToTxnRequest {
115115
types::CompactArray(types::Struct { version }).encode(buf, &self.transactions)?;
116116
} else {
117117
if !self.transactions.is_empty() {
118-
bail!("failed to encode");
118+
bail!("A field is set that is not available on the selected protocol version");
119119
}
120120
}
121121
if version <= 3 {
@@ -126,21 +126,21 @@ impl Encodable for AddPartitionsToTxnRequest {
126126
}
127127
} else {
128128
if !self.v3_and_below_transactional_id.is_empty() {
129-
bail!("failed to encode");
129+
bail!("A field is set that is not available on the selected protocol version");
130130
}
131131
}
132132
if version <= 3 {
133133
types::Int64.encode(buf, &self.v3_and_below_producer_id)?;
134134
} else {
135135
if self.v3_and_below_producer_id != 0 {
136-
bail!("failed to encode");
136+
bail!("A field is set that is not available on the selected protocol version");
137137
}
138138
}
139139
if version <= 3 {
140140
types::Int16.encode(buf, &self.v3_and_below_producer_epoch)?;
141141
} else {
142142
if self.v3_and_below_producer_epoch != 0 {
143-
bail!("failed to encode");
143+
bail!("A field is set that is not available on the selected protocol version");
144144
}
145145
}
146146
if version <= 3 {
@@ -152,7 +152,7 @@ impl Encodable for AddPartitionsToTxnRequest {
152152
}
153153
} else {
154154
if !self.v3_and_below_topics.is_empty() {
155-
bail!("failed to encode");
155+
bail!("A field is set that is not available on the selected protocol version");
156156
}
157157
}
158158
if version >= 3 {
@@ -176,7 +176,7 @@ impl Encodable for AddPartitionsToTxnRequest {
176176
types::CompactArray(types::Struct { version }).compute_size(&self.transactions)?;
177177
} else {
178178
if !self.transactions.is_empty() {
179-
bail!("failed to encode");
179+
bail!("A field is set that is not available on the selected protocol version");
180180
}
181181
}
182182
if version <= 3 {
@@ -188,21 +188,21 @@ impl Encodable for AddPartitionsToTxnRequest {
188188
}
189189
} else {
190190
if !self.v3_and_below_transactional_id.is_empty() {
191-
bail!("failed to encode");
191+
bail!("A field is set that is not available on the selected protocol version");
192192
}
193193
}
194194
if version <= 3 {
195195
total_size += types::Int64.compute_size(&self.v3_and_below_producer_id)?;
196196
} else {
197197
if self.v3_and_below_producer_id != 0 {
198-
bail!("failed to encode");
198+
bail!("A field is set that is not available on the selected protocol version");
199199
}
200200
}
201201
if version <= 3 {
202202
total_size += types::Int16.compute_size(&self.v3_and_below_producer_epoch)?;
203203
} else {
204204
if self.v3_and_below_producer_epoch != 0 {
205-
bail!("failed to encode");
205+
bail!("A field is set that is not available on the selected protocol version");
206206
}
207207
}
208208
if version <= 3 {
@@ -215,7 +215,7 @@ impl Encodable for AddPartitionsToTxnRequest {
215215
}
216216
} else {
217217
if !self.v3_and_below_topics.is_empty() {
218-
bail!("failed to encode");
218+
bail!("A field is set that is not available on the selected protocol version");
219219
}
220220
}
221221
if version >= 3 {
@@ -557,35 +557,35 @@ impl Encodable for AddPartitionsToTxnTransaction {
557557
types::CompactString.encode(buf, &self.transactional_id)?;
558558
} else {
559559
if !self.transactional_id.is_empty() {
560-
bail!("failed to encode");
560+
bail!("A field is set that is not available on the selected protocol version");
561561
}
562562
}
563563
if version >= 4 {
564564
types::Int64.encode(buf, &self.producer_id)?;
565565
} else {
566566
if self.producer_id != 0 {
567-
bail!("failed to encode");
567+
bail!("A field is set that is not available on the selected protocol version");
568568
}
569569
}
570570
if version >= 4 {
571571
types::Int16.encode(buf, &self.producer_epoch)?;
572572
} else {
573573
if self.producer_epoch != 0 {
574-
bail!("failed to encode");
574+
bail!("A field is set that is not available on the selected protocol version");
575575
}
576576
}
577577
if version >= 4 {
578578
types::Boolean.encode(buf, &self.verify_only)?;
579579
} else {
580580
if self.verify_only {
581-
bail!("failed to encode");
581+
bail!("A field is set that is not available on the selected protocol version");
582582
}
583583
}
584584
if version >= 4 {
585585
types::CompactArray(types::Struct { version }).encode(buf, &self.topics)?;
586586
} else {
587587
if !self.topics.is_empty() {
588-
bail!("failed to encode");
588+
bail!("A field is set that is not available on the selected protocol version");
589589
}
590590
}
591591
if version >= 3 {
@@ -608,36 +608,36 @@ impl Encodable for AddPartitionsToTxnTransaction {
608608
total_size += types::CompactString.compute_size(&self.transactional_id)?;
609609
} else {
610610
if !self.transactional_id.is_empty() {
611-
bail!("failed to encode");
611+
bail!("A field is set that is not available on the selected protocol version");
612612
}
613613
}
614614
if version >= 4 {
615615
total_size += types::Int64.compute_size(&self.producer_id)?;
616616
} else {
617617
if self.producer_id != 0 {
618-
bail!("failed to encode");
618+
bail!("A field is set that is not available on the selected protocol version");
619619
}
620620
}
621621
if version >= 4 {
622622
total_size += types::Int16.compute_size(&self.producer_epoch)?;
623623
} else {
624624
if self.producer_epoch != 0 {
625-
bail!("failed to encode");
625+
bail!("A field is set that is not available on the selected protocol version");
626626
}
627627
}
628628
if version >= 4 {
629629
total_size += types::Boolean.compute_size(&self.verify_only)?;
630630
} else {
631631
if self.verify_only {
632-
bail!("failed to encode");
632+
bail!("A field is set that is not available on the selected protocol version");
633633
}
634634
}
635635
if version >= 4 {
636636
total_size +=
637637
types::CompactArray(types::Struct { version }).compute_size(&self.topics)?;
638638
} else {
639639
if !self.topics.is_empty() {
640-
bail!("failed to encode");
640+
bail!("A field is set that is not available on the selected protocol version");
641641
}
642642
}
643643
if version >= 3 {

src/messages/add_partitions_to_txn_response.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl Encodable for AddPartitionsToTxnResponse {
235235
.encode(buf, &self.results_by_transaction)?;
236236
} else {
237237
if !self.results_by_transaction.is_empty() {
238-
bail!("failed to encode");
238+
bail!("A field is set that is not available on the selected protocol version");
239239
}
240240
}
241241
if version <= 3 {
@@ -248,7 +248,7 @@ impl Encodable for AddPartitionsToTxnResponse {
248248
}
249249
} else {
250250
if !self.results_by_topic_v3_and_below.is_empty() {
251-
bail!("failed to encode");
251+
bail!("A field is set that is not available on the selected protocol version");
252252
}
253253
}
254254
if version >= 3 {
@@ -276,7 +276,7 @@ impl Encodable for AddPartitionsToTxnResponse {
276276
.compute_size(&self.results_by_transaction)?;
277277
} else {
278278
if !self.results_by_transaction.is_empty() {
279-
bail!("failed to encode");
279+
bail!("A field is set that is not available on the selected protocol version");
280280
}
281281
}
282282
if version <= 3 {
@@ -289,7 +289,7 @@ impl Encodable for AddPartitionsToTxnResponse {
289289
}
290290
} else {
291291
if !self.results_by_topic_v3_and_below.is_empty() {
292-
bail!("failed to encode");
292+
bail!("A field is set that is not available on the selected protocol version");
293293
}
294294
}
295295
if version >= 3 {
@@ -424,14 +424,14 @@ impl Encodable for AddPartitionsToTxnResult {
424424
types::CompactString.encode(buf, &self.transactional_id)?;
425425
} else {
426426
if !self.transactional_id.is_empty() {
427-
bail!("failed to encode");
427+
bail!("A field is set that is not available on the selected protocol version");
428428
}
429429
}
430430
if version >= 4 {
431431
types::CompactArray(types::Struct { version }).encode(buf, &self.topic_results)?;
432432
} else {
433433
if !self.topic_results.is_empty() {
434-
bail!("failed to encode");
434+
bail!("A field is set that is not available on the selected protocol version");
435435
}
436436
}
437437
if version >= 3 {
@@ -454,15 +454,15 @@ impl Encodable for AddPartitionsToTxnResult {
454454
total_size += types::CompactString.compute_size(&self.transactional_id)?;
455455
} else {
456456
if !self.transactional_id.is_empty() {
457-
bail!("failed to encode");
457+
bail!("A field is set that is not available on the selected protocol version");
458458
}
459459
}
460460
if version >= 4 {
461461
total_size +=
462462
types::CompactArray(types::Struct { version }).compute_size(&self.topic_results)?;
463463
} else {
464464
if !self.topic_results.is_empty() {
465-
bail!("failed to encode");
465+
bail!("A field is set that is not available on the selected protocol version");
466466
}
467467
}
468468
if version >= 3 {

src/messages/alter_partition_request.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ impl Encodable for BrokerState {
212212
types::Int32.encode(buf, &self.broker_id)?;
213213
} else {
214214
if self.broker_id != 0 {
215-
bail!("failed to encode");
215+
bail!("A field is set that is not available on the selected protocol version");
216216
}
217217
}
218218
if version >= 3 {
219219
types::Int64.encode(buf, &self.broker_epoch)?;
220220
} else {
221221
if self.broker_epoch != -1 {
222-
bail!("failed to encode");
222+
bail!("A field is set that is not available on the selected protocol version");
223223
}
224224
}
225225
let num_tagged_fields = self.unknown_tagged_fields.len();
@@ -240,14 +240,14 @@ impl Encodable for BrokerState {
240240
total_size += types::Int32.compute_size(&self.broker_id)?;
241241
} else {
242242
if self.broker_id != 0 {
243-
bail!("failed to encode");
243+
bail!("A field is set that is not available on the selected protocol version");
244244
}
245245
}
246246
if version >= 3 {
247247
total_size += types::Int64.compute_size(&self.broker_epoch)?;
248248
} else {
249249
if self.broker_epoch != -1 {
250-
bail!("failed to encode");
250+
bail!("A field is set that is not available on the selected protocol version");
251251
}
252252
}
253253
let num_tagged_fields = self.unknown_tagged_fields.len();
@@ -422,22 +422,22 @@ impl Encodable for PartitionData {
422422
types::CompactArray(types::Int32).encode(buf, &self.new_isr)?;
423423
} else {
424424
if !self.new_isr.is_empty() {
425-
bail!("failed to encode");
425+
bail!("A field is set that is not available on the selected protocol version");
426426
}
427427
}
428428
if version >= 3 {
429429
types::CompactArray(types::Struct { version })
430430
.encode(buf, &self.new_isr_with_epochs)?;
431431
} else {
432432
if !self.new_isr_with_epochs.is_empty() {
433-
bail!("failed to encode");
433+
bail!("A field is set that is not available on the selected protocol version");
434434
}
435435
}
436436
if version >= 1 {
437437
types::Int8.encode(buf, &self.leader_recovery_state)?;
438438
} else {
439439
if self.leader_recovery_state != 0 {
440-
bail!("failed to encode");
440+
bail!("A field is set that is not available on the selected protocol version");
441441
}
442442
}
443443
types::Int32.encode(buf, &self.partition_epoch)?;
@@ -461,22 +461,22 @@ impl Encodable for PartitionData {
461461
total_size += types::CompactArray(types::Int32).compute_size(&self.new_isr)?;
462462
} else {
463463
if !self.new_isr.is_empty() {
464-
bail!("failed to encode");
464+
bail!("A field is set that is not available on the selected protocol version");
465465
}
466466
}
467467
if version >= 3 {
468468
total_size += types::CompactArray(types::Struct { version })
469469
.compute_size(&self.new_isr_with_epochs)?;
470470
} else {
471471
if !self.new_isr_with_epochs.is_empty() {
472-
bail!("failed to encode");
472+
bail!("A field is set that is not available on the selected protocol version");
473473
}
474474
}
475475
if version >= 1 {
476476
total_size += types::Int8.compute_size(&self.leader_recovery_state)?;
477477
} else {
478478
if self.leader_recovery_state != 0 {
479-
bail!("failed to encode");
479+
bail!("A field is set that is not available on the selected protocol version");
480480
}
481481
}
482482
total_size += types::Int32.compute_size(&self.partition_epoch)?;

0 commit comments

Comments
 (0)