Commit 0df1e398 authored by Anders Carling's avatar Anders Carling

Raise NoMethodError for unknown fields

More informative and more ruby-like
parent 956a770a
......@@ -166,7 +166,7 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
name, name_len);
if (f == NULL) {
rb_raise(rb_eArgError, "Unknown field");
return rb_call_super(argc, argv);
}
if (setter) {
......
......@@ -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])) {
throw context.runtime.newNoMethodError("undefined method `" + args[0].toString() + "' for " + metaClass.toString(), args[0].asJavaString(), metaClass);
}
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)) {
throw context.runtime.newNoMethodError("undefined method `" + args[0].asJavaString() + "' for " + metaClass.toString(), args[0].asJavaString(), metaClass);
}
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;
......
......@@ -191,6 +191,18 @@ module BasicTest
assert m1.hash != m2.hash
end
def test_unknown_field_errors
e = assert_raise NoMethodError do
TestMessage.new.hello
end
assert_match(/hello/, e.message)
e = assert_raise NoMethodError do
TestMessage.new.hello = "world"
end
assert_match(/hello/, e.message)
end
def test_type_errors
m = TestMessage.new
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