Protobuf: changed all exceptions in generated code into error codes. See description of particular codes in "error_codes"
This commit is contained in:
@@ -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)
|
||||||
Binary file not shown.
+12
-13
@@ -21,16 +21,21 @@ void ClassGenerator::generateCode(io::Printer *printer, bool isBuilder) const {
|
|||||||
generateHeader(printer, isBuilder);
|
generateHeader(printer, isBuilder);
|
||||||
printer->Indent();
|
printer->Indent();
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Field generator should know if it is generating code for builder.
|
* Field generator should know if it is generating code for builder
|
||||||
* or for fair class to choose between 'val' and 'var'.
|
* or for fair class to choose between 'val' and 'var'.
|
||||||
* Also note that fields should be declared before init section.
|
* Also note that fields should be declared before init section.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (FieldGenerator *gen: properties) {
|
for (FieldGenerator *gen: properties) {
|
||||||
gen->generateCode(printer, isBuilder);
|
gen->generateCode(printer, isBuilder);
|
||||||
printer->Print("\n");
|
printer->Print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate field for errors code
|
||||||
|
printer->Print("\n");
|
||||||
|
printer->Print("var errorCode: Int = 0\n");
|
||||||
|
|
||||||
printer->Print("\n");
|
printer->Print("\n");
|
||||||
generateInitSection(printer);
|
generateInitSection(printer);
|
||||||
|
|
||||||
@@ -298,13 +303,7 @@ void ClassGenerator::generateParseMethods(io::Printer *printer) const {
|
|||||||
printer->Indent();
|
printer->Indent();
|
||||||
|
|
||||||
// check that wire type of that field is equal to expected
|
// check that wire type of that field is equal to expected
|
||||||
printer->Print(vars, "if (wireType != $kotlinWireType$) {\n");
|
printer->Print(vars, "if (wireType != $kotlinWireType$) { errorCode = 1; return false } \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");
|
|
||||||
|
|
||||||
properties[i]->generateSerializationCode(printer, /* isRead = */ true, /* noTag = */ true);
|
properties[i]->generateSerializationCode(printer, /* isRead = */ true, /* noTag = */ true);
|
||||||
|
|
||||||
@@ -340,7 +339,7 @@ void ClassGenerator::generateParseMethods(io::Printer *printer) const {
|
|||||||
vars["dollar"] = "$";
|
vars["dollar"] = "$";
|
||||||
printer->Print(vars,
|
printer->Print(vars,
|
||||||
"if (getSizeNoTag() > expectedSize) { "
|
"if (getSizeNoTag() > expectedSize) { "
|
||||||
"throw InvalidProtocolBufferException(\"Error: expected size of message $dollar$expectedSize, but have read at least $dollar${getSizeNoTag()}\") "
|
"errorCode = 2 "
|
||||||
"}\n");
|
"}\n");
|
||||||
|
|
||||||
printer->Print("return this\n");
|
printer->Print("return this\n");
|
||||||
|
|||||||
+6
-9
@@ -44,15 +44,14 @@ void EnumGenerator::generateCode(io::Printer * printer) const {
|
|||||||
// Generate enum values.
|
// Generate enum values.
|
||||||
for (int i = 0; i < enumValues.size(); ++i) {
|
for (int i = 0; i < enumValues.size(); ++i) {
|
||||||
enumValues[i]->generateCode(printer);
|
enumValues[i]->generateCode(printer);
|
||||||
if (i + 1 != enumValues.size()) {
|
printer->Print(",");
|
||||||
printer->Print(",");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printer->Print(";"); // semicolon is necessary as companion object will follow
|
|
||||||
}
|
|
||||||
printer->Print("\n");
|
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");
|
printer->Print("\n");
|
||||||
generateEnumConverter(printer);
|
generateEnumConverter(printer);
|
||||||
|
|
||||||
@@ -86,9 +85,7 @@ void EnumGenerator::generateEnumConverter(io::Printer *printer) const {
|
|||||||
|
|
||||||
// catch cast errors in else-clause
|
// catch cast errors in else-clause
|
||||||
printer->Print(vars,
|
printer->Print(vars,
|
||||||
"else -> throw InvalidProtocolBufferException("
|
"else -> Unexpected\n");
|
||||||
"\"Error: got unexpected int $dollar${ord} while parsing $type$ \""
|
|
||||||
");\n");
|
|
||||||
|
|
||||||
printer->Outdent(); // when-clause
|
printer->Outdent(); // when-clause
|
||||||
printer->Print("}\n");
|
printer->Print("}\n");
|
||||||
|
|||||||
+1
-4
@@ -257,10 +257,7 @@ void FieldGenerator::generateSerializationForMessages(io::Printer * printer, boo
|
|||||||
"$fieldName$.mergeFromWithSize(input, expectedSize)\n");
|
"$fieldName$.mergeFromWithSize(input, expectedSize)\n");
|
||||||
|
|
||||||
// check that actual size equal to expected size
|
// check that actual size equal to expected size
|
||||||
printer->Print(vars, "if (expectedSize != $fieldName$.getSizeNoTag()) { "
|
printer->Print(vars, "if (expectedSize != $fieldName$.getSizeNoTag()) { errorCode = 3; return false }\n");
|
||||||
"throw InvalidProtocolBufferException ("
|
|
||||||
"\"Expected size $dollar${expectedSize} got $dollar${$fieldName$.getSizeNoTag()}"
|
|
||||||
"\") }\n");
|
|
||||||
printer->Outdent();
|
printer->Outdent();
|
||||||
printer->Print("}\n");
|
printer->Print("}\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user