Browse Source

introduce errors pkg

pull/456/head
Anton Kaliaev 8 years ago
parent
commit
db69845ded
No known key found for this signature in database GPG Key ID: 7B6881D965918214
5 changed files with 20 additions and 16 deletions
  1. +4
    -3
      client/http_client.go
  2. +2
    -2
      client/ws_client.go
  3. +3
    -2
      server/handlers.go
  4. +8
    -7
      server/http_params.go
  5. +3
    -2
      server/http_server.go

+ 4
- 3
client/http_client.go View File

@ -11,6 +11,7 @@ import (
"reflect"
"strings"
"github.com/pkg/errors"
types "github.com/tendermint/go-rpc/types"
wire "github.com/tendermint/go-wire"
)
@ -143,16 +144,16 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface
response := &types.RPCResponse{}
err = json.Unmarshal(responseBytes, response)
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
if errorStr != "" {
return nil, fmt.Errorf("Response error: %v", errorStr)
return nil, errors.Errorf("Response error: %v", errorStr)
}
// unmarshal the RawMessage into the result
result = wire.ReadJSONPtr(result, *response.Result, &err)
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
}


+ 2
- 2
client/ws_client.go View File

@ -2,12 +2,12 @@ package rpcclient
import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
cmn "github.com/tendermint/go-common"
types "github.com/tendermint/go-rpc/types"
)
@ -104,7 +104,7 @@ func (wsc *WSClient) receiveEventsRoutine() {
continue
}
if response.Error != "" {
wsc.ErrorsCh <- fmt.Errorf(response.Error)
wsc.ErrorsCh <- errors.Errorf(response.Error)
continue
}
wsc.ResultsCh <- *response.Result


+ 3
- 2
server/handlers.go View File

@ -13,6 +13,7 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
cmn "github.com/tendermint/go-common"
events "github.com/tendermint/go-events"
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 !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())
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) {
errV := returns[1]
if errV.Interface() != nil {
return nil, fmt.Errorf("%v", errV.Interface())
return nil, errors.Errorf("%v", errV.Interface())
}
rv := returns[0]
// the result is a registered interface,


+ 8
- 7
server/http_params.go View File

@ -2,10 +2,11 @@ package rpcserver
import (
"encoding/hex"
"fmt"
"net/http"
"regexp"
"strconv"
"github.com/pkg/errors"
)
var (
@ -39,7 +40,7 @@ func GetParamInt64(r *http.Request, param string) (int64, error) {
s := GetParam(r, param)
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, fmt.Errorf(param, err.Error())
return 0, errors.Errorf(param, err.Error())
}
return i, nil
}
@ -48,7 +49,7 @@ func GetParamInt32(r *http.Request, param string) (int32, error) {
s := GetParam(r, param)
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, fmt.Errorf(param, err.Error())
return 0, errors.Errorf(param, err.Error())
}
return int32(i), nil
}
@ -57,7 +58,7 @@ func GetParamUint64(r *http.Request, param string) (uint64, error) {
s := GetParam(r, param)
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, fmt.Errorf(param, err.Error())
return 0, errors.Errorf(param, err.Error())
}
return i, nil
}
@ -66,7 +67,7 @@ func GetParamUint(r *http.Request, param string) (uint, error) {
s := GetParam(r, param)
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, fmt.Errorf(param, err.Error())
return 0, errors.Errorf(param, err.Error())
}
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) {
s := GetParam(r, param)
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
}
@ -83,7 +84,7 @@ func GetParamFloat64(r *http.Request, param string) (float64, error) {
s := GetParam(r, param)
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, fmt.Errorf(param, err.Error())
return 0, errors.Errorf(param, err.Error())
}
return f, nil
}

+ 3
- 2
server/http_server.go View File

@ -11,6 +11,7 @@ import (
"strings"
"time"
"github.com/pkg/errors"
types "github.com/tendermint/go-rpc/types"
)
@ -24,7 +25,7 @@ func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.List
// TODO: Deprecate
proto = types.SocketType(listenAddr)
addr = listenAddr
// return nil, fmt.Errorf("Invalid listener address %s", lisenAddr)
// return nil, errors.Errorf("Invalid listener address %s", lisenAddr)
} else {
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))
listener, err = net.Listen(proto, addr)
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() {


Loading…
Cancel
Save