Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error messages for legacy rest endpoints #7856

Merged
merged 14 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions x/auth/client/rest/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/cosmos/cosmos-sdk/client"
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -56,14 +55,9 @@ func DecodeTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {

response := DecodeResp(stdTx)

_, err = clientCtx.LegacyAmino.MarshalJSON(response)
err = checkForJSONMarshalFailure(w, clientCtx, response)
if err != nil {
if strings.Contains(err.Error(), "unregistered concrete type") {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
"This transaction was created with the new SIGN_MODE_DIRECT signing method, and therefore cannot be displayed"+
" via legacy REST handlers, please use CLI or directly query the Tendermint RPC endpoint to query"+
" this transaction. gRPC gateway endpoint is /cosmos/tx/v1beta1/txs/decode")
}
// Error is already returned by checkForJSONMarshalFailure.
return
}

Expand Down
36 changes: 22 additions & 14 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,9 @@ func QueryTxsRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
packStdTxResponse(w, clientCtx, txRes)
}

_, err = clientCtx.LegacyAmino.MarshalJSON(searchResult)
err = checkForJSONMarshalFailure(w, clientCtx, searchResult)
if err != nil {
if strings.Contains(err.Error(), "unregistered concrete type") {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
"This transaction was created with the new SIGN_MODE_DIRECT signing method, and therefore cannot be displayed"+
" via legacy REST handlers, please use CLI or directly query the Tendermint RPC endpoint to query"+
" this transaction. gRPC gateway endpoint is /cosmos/tx/v1beta1/txs")
}
// Error is already returned by checkForJSONMarshalFailure.
return
}

Expand Down Expand Up @@ -154,14 +149,9 @@ func QueryTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
}

_, err = clientCtx.LegacyAmino.MarshalJSON(output)
err = checkForJSONMarshalFailure(w, clientCtx, output)
if err != nil {
if strings.Contains(err.Error(), "unregistered concrete type") {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
"This transaction was created with the new SIGN_MODE_DIRECT signing method, and therefore cannot be displayed"+
" via legacy REST handlers, please use CLI or directly query the Tendermint RPC endpoint to query"+
" this transaction. gRPC gateway endpoint is /cosmos/tx/v1beta1/tx/<txhash>")
}
// Error is already returned by checkForJSONMarshalFailure.
return
}

Expand Down Expand Up @@ -204,3 +194,21 @@ func packStdTxResponse(w http.ResponseWriter, clientCtx client.Context, txRes *s

return nil
}

func checkForJSONMarshalFailure(w http.ResponseWriter, ctx client.Context, resp interface{}) error {
// LegacyAmino used intentionally here to error message
const errMsg = "unregistered concrete type"
marshaler := ctx.LegacyAmino

_, err := marshaler.MarshalJSON(resp)

alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && strings.Contains(err.Error(), errMsg) {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
"This transaction was created with the new SIGN_MODE_DIRECT signing method, and therefore cannot be displayed"+
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
" via legacy REST handlers, please use CLI or directly query the Tendermint RPC endpoint to query"+
" this transaction.")
return err
}

return nil
}
4 changes: 2 additions & 2 deletions x/auth/client/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (s *IntegrationTestSuite) TestLegacyRestErrMessages() {
"and therefore cannot be displayed via legacy REST handlers, please use CLI or directly query the Tendermint " +
"RPC endpoint to query this transaction."

s.Require().Contains(errResp.Error, errMsg)
s.Require().Equal(errResp.Error, errMsg)

// try fetching the txn using gRPC req, it will fetch info since it has proto codec.
grpcJSON, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/tx/%s", val.APIAddress, txRes.TxHash))
Expand Down Expand Up @@ -369,7 +369,7 @@ func (s *IntegrationTestSuite) TestLegacyRestErrMessages() {
s.Require().NoError(err)

s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(res, &errResp))
s.Require().Contains(errResp.Error, errMsg)
s.Require().Equal(errResp.Error, errMsg)
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down