Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
a3e3235d
Commit
a3e3235d
authored
Dec 12, 2016
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rpc: improve error messages for invalid arguments
The message now includes the index of the invalid arg.
parent
2be3c4b0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
31 deletions
+32
-31
json.go
rpc/json.go
+32
-31
No files found.
rpc/json.go
View file @
a3e3235d
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
rpc
package
rpc
import
(
import
(
"bytes"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"io"
...
@@ -266,42 +267,42 @@ func (c *jsonCodec) ParseRequestArguments(argTypes []reflect.Type, params interf
...
@@ -266,42 +267,42 @@ func (c *jsonCodec) ParseRequestArguments(argTypes []reflect.Type, params interf
}
}
}
}
// parsePositionalArguments tries to parse the given args to an array of values with the given types.
// parsePositionalArguments tries to parse the given args to an array of values with the
// It returns the parsed values or an error when the args could not be parsed. Missing optional arguments
// given types. It returns the parsed values or an error when the args could not be
// are returned as reflect.Zero values.
// parsed. Missing optional arguments are returned as reflect.Zero values.
func
parsePositionalArguments
(
args
json
.
RawMessage
,
callbackArgs
[]
reflect
.
Type
)
([]
reflect
.
Value
,
Error
)
{
func
parsePositionalArguments
(
rawArgs
json
.
RawMessage
,
types
[]
reflect
.
Type
)
([]
reflect
.
Value
,
Error
)
{
params
:=
make
([]
interface
{},
0
,
len
(
callbackArgs
))
// Read beginning of the args array.
for
_
,
t
:=
range
callbackArgs
{
dec
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
rawArgs
))
params
=
append
(
params
,
reflect
.
New
(
t
)
.
Interface
())
if
tok
,
_
:=
dec
.
Token
();
tok
!=
json
.
Delim
(
'['
)
{
return
nil
,
&
invalidParamsError
{
"non-array args"
}
}
}
// Read args.
if
err
:=
json
.
Unmarshal
(
args
,
&
params
);
err
!=
nil
{
args
:=
make
([]
reflect
.
Value
,
0
,
len
(
types
))
return
nil
,
&
invalidParamsError
{
err
.
Error
()}
for
i
:=
0
;
dec
.
More
();
i
++
{
if
i
>=
len
(
types
)
{
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"too many arguments, want at most %d"
,
len
(
types
))}
}
}
argval
:=
reflect
.
New
(
types
[
i
])
if
len
(
params
)
>
len
(
callbackArgs
)
{
if
err
:=
dec
.
Decode
(
argval
.
Interface
());
err
!=
nil
{
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"too many params, want %d got %d"
,
len
(
callbackArgs
),
len
(
params
)
)}
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"invalid argument %d: %v"
,
i
,
err
)}
}
}
if
argval
.
IsNil
()
&&
types
[
i
]
.
Kind
()
!=
reflect
.
Ptr
{
// assume missing params are null values
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"missing value for required argument %d"
,
i
)}
for
i
:=
len
(
params
);
i
<
len
(
callbackArgs
);
i
++
{
params
=
append
(
params
,
nil
)
}
}
args
=
append
(
args
,
argval
.
Elem
())
argValues
:=
make
([]
reflect
.
Value
,
len
(
params
))
}
for
i
,
p
:=
range
params
{
// Read end of args array.
// verify that JSON null values are only supplied for optional arguments (ptr types)
if
_
,
err
:=
dec
.
Token
();
err
!=
nil
{
if
p
==
nil
&&
callbackArgs
[
i
]
.
Kind
()
!=
reflect
.
Ptr
{
return
nil
,
&
invalidParamsError
{
err
.
Error
()}
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"invalid or missing value for params[%d]"
,
i
)}
}
}
if
p
==
nil
{
// Set any missing args to nil.
argValues
[
i
]
=
reflect
.
Zero
(
callbackArgs
[
i
])
for
i
:=
len
(
args
);
i
<
len
(
types
);
i
++
{
}
else
{
// deref pointers values creates previously with reflect.New
if
types
[
i
]
.
Kind
()
!=
reflect
.
Ptr
{
argValues
[
i
]
=
reflect
.
ValueOf
(
p
)
.
Elem
()
return
nil
,
&
invalidParamsError
{
fmt
.
Sprintf
(
"missing value for required argument %d"
,
i
)}
}
}
args
=
append
(
args
,
reflect
.
Zero
(
types
[
i
]))
}
}
return
args
,
nil
return
argValues
,
nil
}
}
// CreateResponse will create a JSON-RPC success response with the given id and reply as result.
// CreateResponse will create a JSON-RPC success response with the given id and reply as result.
...
...
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