diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/error_codes b/proto/compiler/google/src/google/protobuf/compiler/kotlin/error_codes new file mode 100644 index 00000000000..b9e207e6a65 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/error_codes @@ -0,0 +1,3 @@ +1 - mismatch of expected and actual WireType in parseFieldFrom method +2 - read message of size that exceeds expected in parseFromWithSize method +3 - read message of size that is not equal to expected. Can occur when deserializing nested message (code generated in C++ method generateSerializationMethods) diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc index 6083f21eb90..9eddc2bcaea 100755 Binary files a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc and b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc differ diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_class_generator.cc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_class_generator.cc index d857361e478..d4f3127fa0f 100644 --- a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_class_generator.cc +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_class_generator.cc @@ -21,16 +21,21 @@ void ClassGenerator::generateCode(io::Printer *printer, bool isBuilder) const { generateHeader(printer, isBuilder); printer->Indent(); - /** - * Field generator should know if it is generating code for builder. - * or for fair class to choose between 'val' and 'var'. - * Also note that fields should be declared before init section. - */ + /* + * Field generator should know if it is generating code for builder + * or for fair class to choose between 'val' and 'var'. + * Also note that fields should be declared before init section. + */ + for (FieldGenerator *gen: properties) { gen->generateCode(printer, isBuilder); printer->Print("\n"); } + // Generate field for errors code + printer->Print("\n"); + printer->Print("var errorCode: Int = 0\n"); + printer->Print("\n"); generateInitSection(printer); @@ -298,13 +303,7 @@ void ClassGenerator::generateParseMethods(io::Printer *printer) const { printer->Indent(); // check that wire type of that field is equal to expected - printer->Print(vars, "if (wireType != $kotlinWireType$) {\n"); - printer->Indent(); - printer->Print(vars, "throw InvalidProtocolBufferException(\"" - "Error: Field number $fieldNumber$ has wire type $kotlinWireType$" - " but read $dl${wireType.toString()}\")"); - printer->Outdent(); - printer->Print("}\n"); + printer->Print(vars, "if (wireType != $kotlinWireType$) { errorCode = 1; return false } \n"); properties[i]->generateSerializationCode(printer, /* isRead = */ true, /* noTag = */ true); @@ -340,7 +339,7 @@ void ClassGenerator::generateParseMethods(io::Printer *printer) const { vars["dollar"] = "$"; printer->Print(vars, "if (getSizeNoTag() > expectedSize) { " - "throw InvalidProtocolBufferException(\"Error: expected size of message $dollar$expectedSize, but have read at least $dollar${getSizeNoTag()}\") " + "errorCode = 2 " "}\n"); printer->Print("return this\n"); diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_enum_generator.cc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_enum_generator.cc index c49913bcaec..c4fff0c9166 100644 --- a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_enum_generator.cc +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_enum_generator.cc @@ -44,15 +44,14 @@ void EnumGenerator::generateCode(io::Printer * printer) const { // Generate enum values. for (int i = 0; i < enumValues.size(); ++i) { enumValues[i]->generateCode(printer); - if (i + 1 != enumValues.size()) { - printer->Print(","); - } - else { - printer->Print(";"); // semicolon is necessary as companion object will follow - } + printer->Print(","); printer->Print("\n"); } + // Generate additional value that will indicate errors in parsing this enum from int + vars["size"] = std::to_string(enumValues.size()); + printer->Print(vars, "Unexpected($size$);\n"); + printer->Print("\n"); generateEnumConverter(printer); @@ -86,9 +85,7 @@ void EnumGenerator::generateEnumConverter(io::Printer *printer) const { // catch cast errors in else-clause printer->Print(vars, - "else -> throw InvalidProtocolBufferException(" - "\"Error: got unexpected int $dollar${ord} while parsing $type$ \"" - ");\n"); + "else -> Unexpected\n"); printer->Outdent(); // when-clause printer->Print("}\n"); diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc index 8cb441f4bdc..7f32470b660 100644 --- a/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/src/kotlin_field_generator.cc @@ -257,10 +257,7 @@ void FieldGenerator::generateSerializationForMessages(io::Printer * printer, boo "$fieldName$.mergeFromWithSize(input, expectedSize)\n"); // check that actual size equal to expected size - printer->Print(vars, "if (expectedSize != $fieldName$.getSizeNoTag()) { " - "throw InvalidProtocolBufferException (" - "\"Expected size $dollar${expectedSize} got $dollar${$fieldName$.getSizeNoTag()}" - "\") }\n"); + printer->Print(vars, "if (expectedSize != $fieldName$.getSizeNoTag()) { errorCode = 3; return false }\n"); printer->Outdent(); printer->Print("}\n"); }