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);
|
||||
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");
|
||||
|
||||
+6
-9
@@ -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");
|
||||
|
||||
+1
-4
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user