diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2016-02-11 12:41:34 -0800 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2016-02-11 12:41:34 -0800 |
commit | caf1fb7197ee94c07108fc7cfbca07432b185a28 (patch) | |
tree | 83c11422b63312e782bfa722ea774b0b15b33017 /ruby/src/main/java/com/google | |
parent | e088c2cf4b8f1bde0ce39d989d13ab48c3c05480 (diff) | |
parent | 3a5f213cca8dc7a541bd4fe63ea61c7634e44d5a (diff) | |
download | protobuf-caf1fb7197ee94c07108fc7cfbca07432b185a28.tar.gz protobuf-caf1fb7197ee94c07108fc7cfbca07432b185a28.tar.bz2 protobuf-caf1fb7197ee94c07108fc7cfbca07432b185a28.zip |
Merge pull request #997 from anderscarling/better_errors
ruby: Better exception text for common cases
Diffstat (limited to 'ruby/src/main/java/com/google')
-rw-r--r-- | ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index 547ab22c..39213c4d 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -86,14 +86,14 @@ public class RubyMessage extends RubyObject { if (Utils.isMapEntry(fieldDescriptor)) { 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); map.mergeIntoSelf(context, value); maps.put(fieldDescriptor, map); } else if (fieldDescriptor.isRepeated()) { if (!(value instanceof RubyArray)) - throw runtime.newTypeError("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); addRepeatedField(fieldDescriptor, repeatedField); } else { @@ -217,6 +217,9 @@ public class RubyMessage extends RubyObject { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { + if (!hasField(args[0])) { + return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); + } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; @@ -233,6 +236,10 @@ public class RubyMessage extends RubyObject { if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } + + if (!hasField(field)) { + return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); + } return indexSet(context, field, args[1]); } } @@ -435,6 +442,11 @@ public class RubyMessage extends RubyObject { return ret; } + private boolean hasField(IRubyObject fieldName) { + String nameStr = fieldName.asJavaString(); + return this.descriptor.findFieldByName(Utils.escapeIdentifier(nameStr)) != null; + } + private void checkRepeatedFieldType(ThreadContext context, IRubyObject value, Descriptors.FieldDescriptor fieldDescriptor) { Ruby runtime = context.runtime; |