mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-29 14:11:21 +00:00
introduce errors pkg
This commit is contained in:
parent
ff90224ba8
commit
db69845ded
@ -11,6 +11,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
types "github.com/tendermint/go-rpc/types"
|
types "github.com/tendermint/go-rpc/types"
|
||||||
wire "github.com/tendermint/go-wire"
|
wire "github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
@ -143,16 +144,16 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface
|
|||||||
response := &types.RPCResponse{}
|
response := &types.RPCResponse{}
|
||||||
err = json.Unmarshal(responseBytes, response)
|
err = json.Unmarshal(responseBytes, response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Error unmarshalling rpc response: %v", err)
|
return nil, errors.Errorf("Error unmarshalling rpc response: %v", err)
|
||||||
}
|
}
|
||||||
errorStr := response.Error
|
errorStr := response.Error
|
||||||
if errorStr != "" {
|
if errorStr != "" {
|
||||||
return nil, fmt.Errorf("Response error: %v", errorStr)
|
return nil, errors.Errorf("Response error: %v", errorStr)
|
||||||
}
|
}
|
||||||
// unmarshal the RawMessage into the result
|
// unmarshal the RawMessage into the result
|
||||||
result = wire.ReadJSONPtr(result, *response.Result, &err)
|
result = wire.ReadJSONPtr(result, *response.Result, &err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Error unmarshalling rpc response result: %v", err)
|
return nil, errors.Errorf("Error unmarshalling rpc response result: %v", err)
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,12 @@ package rpcclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/pkg/errors"
|
||||||
cmn "github.com/tendermint/go-common"
|
cmn "github.com/tendermint/go-common"
|
||||||
types "github.com/tendermint/go-rpc/types"
|
types "github.com/tendermint/go-rpc/types"
|
||||||
)
|
)
|
||||||
@ -104,7 +104,7 @@ func (wsc *WSClient) receiveEventsRoutine() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if response.Error != "" {
|
if response.Error != "" {
|
||||||
wsc.ErrorsCh <- fmt.Errorf(response.Error)
|
wsc.ErrorsCh <- errors.Errorf(response.Error)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
wsc.ResultsCh <- *response.Result
|
wsc.ResultsCh <- *response.Result
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/pkg/errors"
|
||||||
cmn "github.com/tendermint/go-common"
|
cmn "github.com/tendermint/go-common"
|
||||||
events "github.com/tendermint/go-events"
|
events "github.com/tendermint/go-events"
|
||||||
types "github.com/tendermint/go-rpc/types"
|
types "github.com/tendermint/go-rpc/types"
|
||||||
@ -272,7 +273,7 @@ func nonJsonToArg(ty reflect.Type, arg string) (reflect.Value, error, bool) {
|
|||||||
|
|
||||||
if isHexString {
|
if isHexString {
|
||||||
if !expectingString && !expectingByteSlice {
|
if !expectingString && !expectingByteSlice {
|
||||||
err := fmt.Errorf("Got a hex string arg, but expected '%s'",
|
err := errors.Errorf("Got a hex string arg, but expected '%s'",
|
||||||
ty.Kind().String())
|
ty.Kind().String())
|
||||||
return reflect.ValueOf(nil), err, false
|
return reflect.ValueOf(nil), err, false
|
||||||
}
|
}
|
||||||
@ -567,7 +568,7 @@ func (wm *WebsocketManager) WebsocketHandler(w http.ResponseWriter, r *http.Requ
|
|||||||
func unreflectResult(returns []reflect.Value) (interface{}, error) {
|
func unreflectResult(returns []reflect.Value) (interface{}, error) {
|
||||||
errV := returns[1]
|
errV := returns[1]
|
||||||
if errV.Interface() != nil {
|
if errV.Interface() != nil {
|
||||||
return nil, fmt.Errorf("%v", errV.Interface())
|
return nil, errors.Errorf("%v", errV.Interface())
|
||||||
}
|
}
|
||||||
rv := returns[0]
|
rv := returns[0]
|
||||||
// the result is a registered interface,
|
// the result is a registered interface,
|
||||||
|
@ -2,10 +2,11 @@ package rpcserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -39,7 +40,7 @@ func GetParamInt64(r *http.Request, param string) (int64, error) {
|
|||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseInt(s, 10, 64)
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf(param, err.Error())
|
return 0, errors.Errorf(param, err.Error())
|
||||||
}
|
}
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -48,7 +49,7 @@ func GetParamInt32(r *http.Request, param string) (int32, error) {
|
|||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseInt(s, 10, 32)
|
i, err := strconv.ParseInt(s, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf(param, err.Error())
|
return 0, errors.Errorf(param, err.Error())
|
||||||
}
|
}
|
||||||
return int32(i), nil
|
return int32(i), nil
|
||||||
}
|
}
|
||||||
@ -57,7 +58,7 @@ func GetParamUint64(r *http.Request, param string) (uint64, error) {
|
|||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseUint(s, 10, 64)
|
i, err := strconv.ParseUint(s, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf(param, err.Error())
|
return 0, errors.Errorf(param, err.Error())
|
||||||
}
|
}
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -66,7 +67,7 @@ func GetParamUint(r *http.Request, param string) (uint, error) {
|
|||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
i, err := strconv.ParseUint(s, 10, 64)
|
i, err := strconv.ParseUint(s, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf(param, err.Error())
|
return 0, errors.Errorf(param, err.Error())
|
||||||
}
|
}
|
||||||
return uint(i), nil
|
return uint(i), nil
|
||||||
}
|
}
|
||||||
@ -74,7 +75,7 @@ func GetParamUint(r *http.Request, param string) (uint, error) {
|
|||||||
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
|
func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
|
||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
if !re.MatchString(s) {
|
if !re.MatchString(s) {
|
||||||
return "", fmt.Errorf(param, "Did not match regular expression %v", re.String())
|
return "", errors.Errorf(param, "Did not match regular expression %v", re.String())
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
@ -83,7 +84,7 @@ func GetParamFloat64(r *http.Request, param string) (float64, error) {
|
|||||||
s := GetParam(r, param)
|
s := GetParam(r, param)
|
||||||
f, err := strconv.ParseFloat(s, 64)
|
f, err := strconv.ParseFloat(s, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf(param, err.Error())
|
return 0, errors.Errorf(param, err.Error())
|
||||||
}
|
}
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
types "github.com/tendermint/go-rpc/types"
|
types "github.com/tendermint/go-rpc/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.List
|
|||||||
// TODO: Deprecate
|
// TODO: Deprecate
|
||||||
proto = types.SocketType(listenAddr)
|
proto = types.SocketType(listenAddr)
|
||||||
addr = listenAddr
|
addr = listenAddr
|
||||||
// return nil, fmt.Errorf("Invalid listener address %s", lisenAddr)
|
// return nil, errors.Errorf("Invalid listener address %s", lisenAddr)
|
||||||
} else {
|
} else {
|
||||||
proto, addr = parts[0], parts[1]
|
proto, addr = parts[0], parts[1]
|
||||||
}
|
}
|
||||||
@ -32,7 +33,7 @@ func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.List
|
|||||||
log.Notice(fmt.Sprintf("Starting RPC HTTP server on %s socket %v", proto, addr))
|
log.Notice(fmt.Sprintf("Starting RPC HTTP server on %s socket %v", proto, addr))
|
||||||
listener, err = net.Listen(proto, addr)
|
listener, err = net.Listen(proto, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to listen to %v: %v", listenAddr, err)
|
return nil, errors.Errorf("Failed to listen to %v: %v", listenAddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user