Skip to content

Commit

Permalink
Fix error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tbfleming committed Sep 19, 2018
1 parent 1154f5f commit c7f86bb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/abieos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,8 @@ inline bool json_to_bin(std::vector<char>& bin, const abi_type* type, const jval
else if (entry.type->filled_struct) {
if (entry.position >= 0 && entry.position < (int)entry.type->fields.size())
s += "." + entry.type->fields[entry.position].name;
} else if (entry.type->optional_of) {
s += "<optional>";
} else if (entry.type->filled_variant) {
s += "<variant>";
} else {
Expand Down Expand Up @@ -1854,10 +1856,11 @@ inline bool json_to_bin(pseudo_object*, jvalue_to_bin_state& state, bool allow_e
if (trace_jvalue_to_bin)
printf("%*s{ %d fields, allow_ex=%d\n", int(state.stack.size() * 4), "", int(type->fields.size()),
allow_extensions);
state.stack.push_back({type, allow_extensions, state.received_value, 0});
state.stack.push_back({type, allow_extensions, state.received_value, -1});
return true;
}
auto& stack_entry = state.stack.back();
++stack_entry.position;
if (stack_entry.position == (int)type->fields.size()) {
if (trace_jvalue_to_bin)
printf("%*s}\n", int((state.stack.size() - 1) * 4), "");
Expand All @@ -1870,12 +1873,12 @@ inline bool json_to_bin(pseudo_object*, jvalue_to_bin_state& state, bool allow_e
if (trace_jvalue_to_bin)
printf("%*sfield %d/%d: %s (event %d)\n", int(state.stack.size() * 4), "", int(stack_entry.position),
int(type->fields.size()), std::string{field.name}.c_str(), (int)event);
++stack_entry.position;
if (it == obj.end()) {
if (field.type->extension_of && allow_extensions) {
state.skipped_extension = true;
return true;
}
stack_entry.position = -1;
throw std::runtime_error("expected field \"" + field.name + "\"");
}
if (state.skipped_extension)
Expand All @@ -1894,18 +1897,19 @@ inline bool json_to_bin(pseudo_array*, jvalue_to_bin_state& state, bool, const a
printf("%*s[ %d elements\n", int(state.stack.size() * 4), "",
int(boost::get<jarray>(state.received_value->value).size()));
push_varuint32(state.bin, boost::get<jarray>(state.received_value->value).size());
state.stack.push_back({type, false, state.received_value, 0});
state.stack.push_back({type, false, state.received_value, -1});
return true;
}
auto& stack_entry = state.stack.back();
auto& arr = boost::get<jarray>(stack_entry.value->value);
++stack_entry.position;
if (stack_entry.position == (int)arr.size()) {
if (trace_jvalue_to_bin)
printf("%*s]\n", int((state.stack.size() - 1) * 4), "");
state.stack.pop_back();
return true;
}
state.received_value = &arr[stack_entry.position++];
state.received_value = &arr[stack_entry.position];
if (trace_jvalue_to_bin)
printf("%*sitem (event %d)\n", int(state.stack.size() * 4), "", (int)event);
return type->array_of->ser &&
Expand All @@ -1918,8 +1922,10 @@ inline bool json_to_bin(pseudo_variant*, jvalue_to_bin_state& state, bool allow_
if (!state.received_value || !boost::get<jarray>(&state.received_value->value))
throw std::runtime_error(R"(expected variant: ["type", value])");
auto& arr = boost::get<jarray>(state.received_value->value);
if (arr.size() != 2)
throw std::runtime_error(R"(expected variant: ["type", value])");
auto* typeName = boost::get<std::string>(&arr[0].value);
if (arr.size() != 2 || !typeName)
if (!typeName)
throw std::runtime_error(R"(expected variant: ["type", value])");
if (trace_jvalue_to_bin)
printf("%*s[ variant %s\n", int(state.stack.size() * 4), "", typeName->c_str());
Expand Down Expand Up @@ -2003,10 +2009,13 @@ inline bool json_to_bin(std::vector<char>& bin, const abi_type* type, std::strin
else if (entry.type->filled_struct) {
if (entry.position >= 0 && entry.position < (int)entry.type->fields.size())
s += "." + entry.type->fields[entry.position].name;
} else if (entry.type->optional_of) {
s += "<optional>";
} else if (entry.type->filled_variant) {
s += "<variant>";
} else
} else {
s += "<?>";
}
}
if (!s.empty())
s += ": ";
Expand Down
87 changes: 87 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,93 @@ void check_types() {
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true,"x3":{"c1":0,"c2":[],"c3":7}},{"x1":null} ]}} )");
});

check_error(context, "expected object",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s4", "null"); });
check_error(context, "expected object",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s4", "[]"); });
check_error(context, R"(s4.a1: expected number or boolean)",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s4", R"({"a1":[]})"); });
check_error(context, R"(expected variant: ["type", value])",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "v1", "null"); });
check_error(context, R"(expected variant: ["type", value])",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "v1", "[]"); });
check_error(context, R"(expected variant: ["type", value])",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "v1", R"(["x",7,5])"); });
check_error(context, R"(<variant>: type is not valid for this variant)",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "v1", R"(["x",7])"); });
check_error(context, R"(expected variant: ["type", value])",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "v1", R"(["int8",7,5])"); });
check_error(context, R"(s5: expected field "x1")",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({})"); });
check_error(context, R"(s5: expected field "x2")",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":5})"); });
check_error(context, R"(s5: expected field "x3")",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":5,"x2":7})"); });
check_error(context, R"(s5.x1: expected number or boolean)",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":null})"); });
check_error(context, R"(s5.x2: expected number or boolean)",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":null})"); });
check_error(context, R"(s5: expected field "x3")",
[&] { return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10})"); });
check_error(context, R"(s5.x3: expected object)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10,"x3":null})");
});
check_error(context, R"(s5.x3: expected field "c1")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10,"x3":{}})");
});
check_error(context, R"(s5.x3: expected field "c2")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10,"x3":{"c1":4}})");
});
check_error(context, R"(s5.x3.c2: expected array)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":{}}})");
});
check_error(context, R"(s5.x3.c2[0]: expected object)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5", R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[7]}})");
});
check_error(context, R"(s5.x3.c2[0]: expected field "x1")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{}]}})");
});
check_error(context, R"(s5.x3.c2[0].x1: expected number or boolean)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":null}]}} )");
});
check_error(context, R"(s5.x3.c2[0]: expected field "x2")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7}]}} )");
});
check_error(context, R"(s5.x3.c2[0]: expected field "x2")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x3":null}]}} )");
});
check_error(context, R"(s5.x3.c2[0].x2: expected number or boolean)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":null}]}} )");
});
check_error(context, R"(s5.x3.c2[0]: expected field "x3")", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true}]}} )");
});
check_error(context, R"(s5.x3.c2[0].x3: expected object)", [&] {
return abieos_json_to_bin_reorderable(context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true,"x3":null}]}} )");
});
check_error(context, R"(s5.x3.c2[1]: expected object)", [&] {
return abieos_json_to_bin_reorderable(
context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true,"x3":{"c1":0,"c2":[],"c3":7}},null]}} )");
});
check_error(context, R"(s5.x3.c2[1]: expected field "x1")", [&] {
return abieos_json_to_bin_reorderable(
context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true,"x3":{"c1":0,"c2":[],"c3":7}},{} ]}} )");
});
check_error(context, R"(s5.x3.c2[1].x1: expected number or boolean)", [&] {
return abieos_json_to_bin_reorderable(
context, testAbiName, "s5",
R"({"x1":9,"x2":10,"x3":{"c1":4,"c2":[{"x1":7,"x2":true,"x3":{"c1":0,"c2":[],"c3":7}},{"x1":null} ]}} )");
});

auto testWith = [&](auto& abiName) {
check_type(context, abiName, "v1", R"(["int8",7])");
check_type(context, abiName, "v1", R"(["s1",{"x1":6}])");
Expand Down

0 comments on commit c7f86bb

Please sign in to comment.