Browse Source

rpc: Use Wrap instead of Errorf error (#3686)

So we can have an error code when rpc fails.

* Update CHANGELOG_PENDING.md
Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>

* wrap correct error
* format code
pull/3720/head
Haochuan Guo 6 years ago
committed by Alexander Simmerl
parent
commit
a7e8fbf3a7
2 changed files with 10 additions and 7 deletions
  1. +2
    -0
      CHANGELOG_PENDING.md
  2. +8
    -7
      rpc/lib/client/http_client.go

+ 2
- 0
CHANGELOG_PENDING.md View File

@ -24,4 +24,6 @@
### IMPROVEMENTS: ### IMPROVEMENTS:
- [p2p] \#3666 Add per channel telemtry to improve reactor observability - [p2p] \#3666 Add per channel telemtry to improve reactor observability
* [rpc] [\#3686](https://github.com/tendermint/tendermint/pull/3686) `HTTPClient#Call` returns wrapped errors, so a caller could use `errors.Cause` to retrieve an error code.
### BUG FIXES: ### BUG FIXES:

+ 8
- 7
rpc/lib/client/http_client.go View File

@ -13,9 +13,10 @@ import (
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
amino "github.com/tendermint/go-amino" amino "github.com/tendermint/go-amino"
cmn "github.com/tendermint/tendermint/libs/common"
cmn "github.com/tendermint/tendermint/libs/common"
types "github.com/tendermint/tendermint/rpc/lib/types" types "github.com/tendermint/tendermint/rpc/lib/types"
) )
@ -303,10 +304,10 @@ func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte, expectedID t
response := &types.RPCResponse{} response := &types.RPCResponse{}
err = json.Unmarshal(responseBytes, response) err = json.Unmarshal(responseBytes, response)
if err != nil { if err != nil {
return nil, errors.Errorf("error unmarshalling rpc response: %v", err)
return nil, errors.Wrap(err, "error unmarshalling rpc response")
} }
if response.Error != nil { if response.Error != nil {
return nil, errors.Errorf("response error: %v", response.Error)
return nil, errors.Wrap(response.Error, "response error")
} }
// From the JSON-RPC 2.0 spec: // From the JSON-RPC 2.0 spec:
// id: It MUST be the same as the value of the id member in the Request Object. // id: It MUST be the same as the value of the id member in the Request Object.
@ -316,7 +317,7 @@ func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte, expectedID t
// Unmarshal the RawMessage into the result. // Unmarshal the RawMessage into the result.
err = cdc.UnmarshalJSON(response.Result, result) err = cdc.UnmarshalJSON(response.Result, result)
if err != nil { if err != nil {
return nil, errors.Errorf("error unmarshalling rpc response result: %v", err)
return nil, errors.Wrap(err, "error unmarshalling rpc response result")
} }
return result, nil return result, nil
} }
@ -328,7 +329,7 @@ func unmarshalResponseBytesArray(cdc *amino.Codec, responseBytes []byte, expecte
) )
err = json.Unmarshal(responseBytes, &responses) err = json.Unmarshal(responseBytes, &responses)
if err != nil { if err != nil {
return nil, errors.Errorf("error unmarshalling rpc response: %v", err)
return nil, errors.Wrap(err, "error unmarshalling rpc response")
} }
// No response error checking here as there may be a mixture of successful // No response error checking here as there may be a mixture of successful
// and unsuccessful responses. // and unsuccessful responses.
@ -341,10 +342,10 @@ func unmarshalResponseBytesArray(cdc *amino.Codec, responseBytes []byte, expecte
// From the JSON-RPC 2.0 spec: // From the JSON-RPC 2.0 spec:
// id: It MUST be the same as the value of the id member in the Request Object. // id: It MUST be the same as the value of the id member in the Request Object.
if err := validateResponseID(&response, expectedID); err != nil { if err := validateResponseID(&response, expectedID); err != nil {
return nil, errors.Errorf("failed to validate response ID in response %d: %v", i, err)
return nil, errors.Wrapf(err, "failed to validate response ID in response %d", i)
} }
if err := cdc.UnmarshalJSON(responses[i].Result, results[i]); err != nil { if err := cdc.UnmarshalJSON(responses[i].Result, results[i]); err != nil {
return nil, errors.Errorf("error unmarshalling rpc response result: %v", err)
return nil, errors.Wrap(err, "error unmarshalling rpc response result")
} }
} }
return results, nil return results, nil


Loading…
Cancel
Save