Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
caf1fb71
Commit
caf1fb71
authored
Feb 11, 2016
by
Joshua Haberman
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #997 from anderscarling/better_errors
ruby: Better exception text for common cases
parents
e088c2cf
3a5f213c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
6 deletions
+47
-6
message.c
ruby/ext/google/protobuf_c/message.c
+4
-4
RubyMessage.java
.../src/main/java/com/google/protobuf/jruby/RubyMessage.java
+14
-2
basic.rb
ruby/tests/basic.rb
+29
-0
No files found.
ruby/ext/google/protobuf_c/message.c
View file @
caf1fb71
...
...
@@ -166,7 +166,7 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
name
,
name_len
);
if
(
f
==
NULL
)
{
r
b_raise
(
rb_eArgError
,
"Unknown field"
);
r
eturn
rb_call_super
(
argc
,
argv
);
}
if
(
setter
)
{
...
...
@@ -197,7 +197,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
f
=
upb_msgdef_ntofz
(
self
->
descriptor
->
msgdef
,
name
);
if
(
f
==
NULL
)
{
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
))
{
...
...
@@ -205,7 +205,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
if
(
TYPE
(
val
)
!=
T_HASH
)
{
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_merge_into_self
(
map
,
val
);
...
...
@@ -214,7 +214,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
if
(
TYPE
(
val
)
!=
T_ARRAY
)
{
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
);
for
(
int
i
=
0
;
i
<
RARRAY_LEN
(
val
);
i
++
)
{
...
...
ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
View file @
caf1fb71
...
...
@@ -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
.
new
TypeError
(
"Expected array as initializer var for repeated field
."
);
throw
runtime
.
new
ArgumentError
(
"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
;
...
...
ruby/tests/basic.rb
View file @
caf1fb71
...
...
@@ -191,6 +191,35 @@ 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_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
m
=
TestMessage
.
new
assert_raise
TypeError
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment