Browse Source

Merge pull request #767 from silasdavis/do-not-swallow

Make RPCError an actual error and don't swallow its companion data
pull/737/merge
Ethan Buchman 7 years ago
committed by GitHub
parent
commit
60b7f2c61b
4 changed files with 27 additions and 3 deletions
  1. +1
    -1
      rpc/lib/client/http_client.go
  2. +1
    -1
      rpc/lib/client/ws_client.go
  3. +8
    -1
      rpc/lib/types/types.go
  4. +17
    -0
      rpc/lib/types/types_test.go

+ 1
- 1
rpc/lib/client/http_client.go View File

@ -147,7 +147,7 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface
return nil, errors.Errorf("Error unmarshalling rpc response: %v", err) return nil, errors.Errorf("Error unmarshalling rpc response: %v", err)
} }
if response.Error != nil { if response.Error != nil {
return nil, errors.Errorf("Response error: %v", response.Error.Message)
return nil, errors.Errorf("Response error: %v", response.Error)
} }
// unmarshal the RawMessage into the result // unmarshal the RawMessage into the result
err = json.Unmarshal(*response.Result, result) err = json.Unmarshal(*response.Result, result)


+ 1
- 1
rpc/lib/client/ws_client.go View File

@ -437,7 +437,7 @@ func (c *WSClient) readRoutine() {
continue continue
} }
if response.Error != nil { if response.Error != nil {
c.ErrorsCh <- errors.New(response.Error.Message)
c.ErrorsCh <- response.Error
continue continue
} }
c.Logger.Info("got response", "resp", response.Result) c.Logger.Info("got response", "resp", response.Result)


+ 8
- 1
rpc/lib/types/types.go View File

@ -6,7 +6,6 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
events "github.com/tendermint/tmlibs/events" events "github.com/tendermint/tmlibs/events"
) )
@ -60,6 +59,14 @@ type RPCError struct {
Data string `json:"data,omitempty"` Data string `json:"data,omitempty"`
} }
func (err RPCError) Error() string {
const baseFormat = "RPC error %v - %s"
if err.Data != "" {
return fmt.Sprintf(baseFormat+": %s", err.Code, err.Message, err.Data)
}
return fmt.Sprintf(baseFormat, err.Code, err.Message)
}
type RPCResponse struct { type RPCResponse struct {
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
ID string `json:"id"` ID string `json:"id"`


+ 17
- 0
rpc/lib/types/types_test.go View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -30,3 +32,18 @@ func TestResponses(t *testing.T) {
i := `{"jsonrpc":"2.0","id":"2","error":{"code":-32601,"message":"Method not found"}}` i := `{"jsonrpc":"2.0","id":"2","error":{"code":-32601,"message":"Method not found"}}`
assert.Equal(string(h), string(i)) assert.Equal(string(h), string(i))
} }
func TestRPCError(t *testing.T) {
assert.Equal(t, "RPC error 12 - Badness: One worse than a code 11",
fmt.Sprintf("%v", &RPCError{
Code: 12,
Message: "Badness",
Data: "One worse than a code 11",
}))
assert.Equal(t, "RPC error 12 - Badness",
fmt.Sprintf("%v", &RPCError{
Code: 12,
Message: "Badness",
}))
}

Loading…
Cancel
Save