diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-13 08:07:03 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-13 08:07:03 +0800 |
commit | 094f921e5028fc215efbc86118e3d3e5b0663055 (patch) | |
tree | ff99596fcb504014b33191f09ba73fd4c18d55cb /rpc/http.go | |
parent | 14bdcd2c052214ca78c7cb163771c780e2fd1291 (diff) | |
download | dexon-094f921e5028fc215efbc86118e3d3e5b0663055.tar.gz dexon-094f921e5028fc215efbc86118e3d3e5b0663055.tar.zst dexon-094f921e5028fc215efbc86118e3d3e5b0663055.zip |
Convert to proper errors
Allow returning different JSON RPC error codes depending on error type
Diffstat (limited to 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/rpc/http.go b/rpc/http.go index 857cf3221..8b45319ff 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -25,22 +25,51 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { rpchttplogger.DebugDetailln("Handling request") 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}) return } reqParsed, reqerr := json.ParseRequestBody(req) - if reqerr != nil { - jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"} + switch reqerr.(type) { + 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}) return } var response interface{} reserr := api.GetRequestReply(&reqParsed, &response) - if reserr != nil { - rpchttplogger.Warnln(reserr) + switch reserr.(type) { + 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()} json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) return |