Commit 0559f3ee authored by Anders Carling's avatar Anders Carling

Add field name to initialization map exceptions

parent 8bcd0d7f
...@@ -197,7 +197,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { ...@@ -197,7 +197,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
f = upb_msgdef_ntofz(self->descriptor->msgdef, name); f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
if (f == NULL) { if (f == NULL) {
rb_raise(rb_eArgError, rb_raise(rb_eArgError,
"Unknown field name in initialization map entry."); "Unknown field name '%s' in initialization map entry.", name);
} }
if (is_map_field(f)) { if (is_map_field(f)) {
...@@ -205,7 +205,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { ...@@ -205,7 +205,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
if (TYPE(val) != T_HASH) { if (TYPE(val) != T_HASH) {
rb_raise(rb_eArgError, rb_raise(rb_eArgError,
"Expected Hash object as initializer value for map field."); "Expected Hash object as initializer value for map field '%s'.", name);
} }
map = layout_get(self->descriptor->layout, Message_data(self), f); map = layout_get(self->descriptor->layout, Message_data(self), f);
Map_merge_into_self(map, val); Map_merge_into_self(map, val);
...@@ -214,7 +214,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { ...@@ -214,7 +214,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
if (TYPE(val) != T_ARRAY) { if (TYPE(val) != T_ARRAY) {
rb_raise(rb_eArgError, rb_raise(rb_eArgError,
"Expected array as initializer value for repeated field."); "Expected array as initializer value for repeated field '%s'.", name);
} }
ary = layout_get(self->descriptor->layout, Message_data(self), f); ary = layout_get(self->descriptor->layout, Message_data(self), f);
for (int i = 0; i < RARRAY_LEN(val); i++) { for (int i = 0; i < RARRAY_LEN(val); i++) {
......
...@@ -86,14 +86,14 @@ public class RubyMessage extends RubyObject { ...@@ -86,14 +86,14 @@ public class RubyMessage extends RubyObject {
if (Utils.isMapEntry(fieldDescriptor)) { if (Utils.isMapEntry(fieldDescriptor)) {
if (!(value instanceof RubyHash)) if (!(value instanceof RubyHash))
throw runtime.newArgumentError("Expected Hash object as initializer value for map field."); throw runtime.newArgumentError("Expected Hash object as initializer value for map field '" + key.asJavaString() + "'.");
final RubyMap map = newMapForField(context, fieldDescriptor); final RubyMap map = newMapForField(context, fieldDescriptor);
map.mergeIntoSelf(context, value); map.mergeIntoSelf(context, value);
maps.put(fieldDescriptor, map); maps.put(fieldDescriptor, map);
} else if (fieldDescriptor.isRepeated()) { } else if (fieldDescriptor.isRepeated()) {
if (!(value instanceof RubyArray)) if (!(value instanceof RubyArray))
throw runtime.newArgumentError("Expected array as initializer var for repeated field."); throw runtime.newArgumentError("Expected array as initializer value for repeated field '" + key.asJavaString() + "'.");
RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, value); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, value);
addRepeatedField(fieldDescriptor, repeatedField); addRepeatedField(fieldDescriptor, repeatedField);
} else { } else {
......
...@@ -203,6 +203,23 @@ module BasicTest ...@@ -203,6 +203,23 @@ module BasicTest
assert_match(/hello/, e.message) assert_match(/hello/, e.message)
end end
def test_initialization_map_errors
e = assert_raise ArgumentError do
TestMessage.new(:hello => "world")
end
assert_match(/hello/, e.message)
e = assert_raise ArgumentError do
MapMessage.new(:map_string_int32 => "hello")
end
assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'."
e = assert_raise ArgumentError do
TestMessage.new(:repeated_uint32 => "hello")
end
assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'."
end
def test_type_errors def test_type_errors
m = TestMessage.new m = TestMessage.new
assert_raise TypeError do assert_raise TypeError do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment