Commit 094f921e authored by Taylor Gerring's avatar Taylor Gerring

Convert to proper errors

Allow returning different JSON RPC error codes depending on error type
parent 14bdcd2c
...@@ -578,7 +578,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -578,7 +578,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
} }
return p.Call(args, reply) return p.Call(args, reply)
case "eth_flush": case "eth_flush":
return errNotImplemented return NewNotImplementedError(req.Method)
case "eth_getBlockByHash": case "eth_getBlockByHash":
args := new(GetBlockByHashArgs) args := new(GetBlockByHashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -618,7 +618,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -618,7 +618,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err return err
} }
if args.Index > int64(len(v.Transactions)) || args.Index < 0 { if args.Index > int64(len(v.Transactions)) || args.Index < 0 {
return NewErrorWithMessage(errDecodeArgs, "Transaction index does not exist") return NewValidationError("Index", "does not exist")
} }
*reply = v.Transactions[args.Index] *reply = v.Transactions[args.Index]
case "eth_getTransactionByBlockNumberAndIndex": case "eth_getTransactionByBlockNumberAndIndex":
...@@ -632,7 +632,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -632,7 +632,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err return err
} }
if args.Index > int64(len(v.Transactions)) || args.Index < 0 { if args.Index > int64(len(v.Transactions)) || args.Index < 0 {
return NewErrorWithMessage(errDecodeArgs, "Transaction index does not exist") return NewValidationError("Index", "does not exist")
} }
*reply = v.Transactions[args.Index] *reply = v.Transactions[args.Index]
case "eth_getUncleByBlockHashAndIndex": case "eth_getUncleByBlockHashAndIndex":
...@@ -646,7 +646,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -646,7 +646,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err return err
} }
if args.Index > int64(len(v.Uncles)) || args.Index < 0 { if args.Index > int64(len(v.Uncles)) || args.Index < 0 {
return NewErrorWithMessage(errDecodeArgs, "Uncle index does not exist") return NewValidationError("Index", "does not exist")
} }
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
...@@ -665,7 +665,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -665,7 +665,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return err return err
} }
if args.Index > int64(len(v.Uncles)) || args.Index < 0 { if args.Index > int64(len(v.Uncles)) || args.Index < 0 {
return NewErrorWithMessage(errDecodeArgs, "Uncle index does not exist") return NewValidationError("Index", "does not exist")
} }
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
...@@ -678,7 +678,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -678,7 +678,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
case "eth_compileSolidity": case "eth_compileSolidity":
case "eth_compileLLL": case "eth_compileLLL":
case "eth_compileSerpent": case "eth_compileSerpent":
return errNotImplemented return NewNotImplementedError(req.Method)
case "eth_newFilter": case "eth_newFilter":
args := new(FilterOptions) args := new(FilterOptions)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -717,7 +717,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -717,7 +717,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return p.AllLogs(args, reply) return p.AllLogs(args, reply)
case "eth_getWork": case "eth_getWork":
case "eth_submitWork": case "eth_submitWork":
return errNotImplemented return NewNotImplementedError(req.Method)
case "db_put": case "db_put":
args := new(DbArgs) args := new(DbArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -746,7 +746,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -746,7 +746,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return p.HasWhisperIdentity(args.Identity, reply) return p.HasWhisperIdentity(args.Identity, reply)
case "shh_newGroup": case "shh_newGroup":
case "shh_addToGroup": case "shh_addToGroup":
return errNotImplemented return NewNotImplementedError(req.Method)
case "shh_newFilter": case "shh_newFilter":
args := new(WhisperFilterArgs) args := new(WhisperFilterArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -790,7 +790,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error ...@@ -790,7 +790,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
// } // }
// return p.WatchTx(args, reply) // return p.WatchTx(args, reply)
default: default:
return NewErrorWithMessage(errNotImplemented, req.Method) return NewNotImplementedError(req.Method)
} }
rpclogger.DebugDetailf("Reply: %T %s", reply, reply) rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
......
This diff is collapsed.
...@@ -25,22 +25,51 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { ...@@ -25,22 +25,51 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
rpchttplogger.DebugDetailln("Handling request") rpchttplogger.DebugDetailln("Handling request")
if req.ContentLength > maxSizeReqLength { if req.ContentLength > maxSizeReqLength {
jsonerr := &RpcErrorObject{-32700, "Error: Request too large"} jsonerr := &RpcErrorObject{-32700, "Request too large"}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return return
} }
reqParsed, reqerr := json.ParseRequestBody(req) reqParsed, reqerr := json.ParseRequestBody(req)
if reqerr != nil { switch reqerr.(type) {
jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"} case nil:
break
case *DecodeParamError:
jsonerr := &RpcErrorObject{-32602, reqerr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return
case *InsufficientParamsError:
jsonerr := &RpcErrorObject{-32602, reqerr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return
case *ValidationError:
jsonerr := &RpcErrorObject{-32602, reqerr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return
default:
jsonerr := &RpcErrorObject{-32700, "Could not parse request"}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return return
} }
var response interface{} var response interface{}
reserr := api.GetRequestReply(&reqParsed, &response) reserr := api.GetRequestReply(&reqParsed, &response)
if reserr != nil { switch reserr.(type) {
rpchttplogger.Warnln(reserr) case nil:
break
case *NotImplementedError:
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
return
case *InsufficientParamsError:
jsonerr := &RpcErrorObject{-32602, reserr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
return
case *ValidationError:
jsonerr := &RpcErrorObject{-32602, reserr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
return
default:
jsonerr := &RpcErrorObject{-32603, reserr.Error()} jsonerr := &RpcErrorObject{-32603, reserr.Error()}
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
return return
......
...@@ -18,16 +18,69 @@ package rpc ...@@ -18,16 +18,69 @@ package rpc
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
) )
var ( type InsufficientParamsError struct {
errArguments = errors.New("Error: Insufficient arguments") have int
errNotImplemented = errors.New("Error: Method not implemented") want int
errUnknown = errors.New("Error: Unknown error") }
errDecodeArgs = errors.New("Error: Could not decode arguments")
) func (e *InsufficientParamsError) Error() string {
return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have)
}
func NewInsufficientParamsError(have int, want int) *InsufficientParamsError {
return &InsufficientParamsError{
have: have,
want: want,
}
}
type NotImplementedError struct {
Method string
}
func (e *NotImplementedError) Error() string {
return fmt.Sprintf("%s method not implemented", e.Method)
}
func NewNotImplementedError(method string) *NotImplementedError {
return &NotImplementedError{
Method: method,
}
}
type DecodeParamError struct {
err string
}
func (e *DecodeParamError) Error() string {
return fmt.Sprintf("could not decode, %s", e.err)
}
func NewDecodeParamError(errstr string) error {
return &DecodeParamError{
err: errstr,
}
}
type ValidationError struct {
ParamName string
msg string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg)
}
func NewValidationError(param string, msg string) error {
return &ValidationError{
ParamName: param,
msg: msg,
}
}
type RpcRequest struct { type RpcRequest struct {
ID interface{} `json:"id"` ID interface{} `json:"id"`
...@@ -53,35 +106,3 @@ type RpcErrorObject struct { ...@@ -53,35 +106,3 @@ type RpcErrorObject struct {
Message string `json:"message"` Message string `json:"message"`
// Data interface{} `json:"data"` // Data interface{} `json:"data"`
} }
func NewErrorWithMessage(err error, msg string) error {
return fmt.Errorf("%s: %s", err.Error(), msg)
}
// func (req *RpcRequest) ToRegisterArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }
// func (req *RpcRequest) ToWatchTxArgs() (string, error) {
// if len(req.Params) < 1 {
// return "", errArguments
// }
// var args string
// err := json.Unmarshal(req.Params, &args)
// if err != nil {
// return "", err
// }
// return args, nil
// }
...@@ -42,7 +42,7 @@ type JsonWrapper struct{} ...@@ -42,7 +42,7 @@ type JsonWrapper struct{}
func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error) { func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error) {
var data []json.RawMessage var data []json.RawMessage
if err = json.Unmarshal(b, &data); err != nil && len(data) == 0 { if err = json.Unmarshal(b, &data); err != nil && len(data) == 0 {
return errDecodeArgs return NewDecodeParamError(err.Error())
} }
// Number index determines the index in the array for a possible block number // Number index determines the index in the array for a possible block number
...@@ -74,7 +74,7 @@ func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error ...@@ -74,7 +74,7 @@ func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error
fallthrough fallthrough
default: default:
if err = json.Unmarshal(data[0], iface); err != nil { if err = json.Unmarshal(data[0], iface); err != nil {
return errDecodeArgs return NewDecodeParamError(err.Error())
} }
numberIndex = 1 numberIndex = 1
} }
...@@ -82,7 +82,7 @@ func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error ...@@ -82,7 +82,7 @@ func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error
// <0 index means out of bound for block number // <0 index means out of bound for block number
if numberIndex >= 0 && len(data) > numberIndex { if numberIndex >= 0 && len(data) > numberIndex {
if err = blockNumber(data[numberIndex], number); err != nil { if err = blockNumber(data[numberIndex], number); err != nil {
return errDecodeArgs return NewDecodeParamError(err.Error())
} }
} }
......
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