diff --git a/CHANGELOG.md b/CHANGELOG.md index 939bea0de042..19203677ad69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -186,6 +186,7 @@ functionality that requires an online connection. * (types/module) [\#5724](https://github.com/cosmos/cosmos-sdk/issues/5724) The `types/module` package does no longer depend on `x/simulation`. * (client) [\#5856](https://github.com/cosmos/cosmos-sdk/pull/5856) Added the possibility to set `--offline` flag with config command. * (client) [\#5895](https://github.com/cosmos/cosmos-sdk/issues/5895) show config options in the config command's help screen. +* (types/rest) [\#5900](https://github.com/cosmos/cosmos-sdk/pull/5900) Add Check*Error function family to spare developers from replicating tons of boilerplate code. ## [v0.38.2] - 2020-03-25 diff --git a/client/rpc/block.go b/client/rpc/block.go index bce145370c33..4d52ac781527 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -136,8 +136,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } output, err := getBlock(cliCtx, &height) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -149,8 +148,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { output, err := getBlock(cliCtx, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/client/rpc/status.go b/client/rpc/status.go index 330d595d5736..da530a0ce2f4 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -76,8 +76,7 @@ type NodeInfoResponse struct { func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { status, err := getNodeStatus(cliCtx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -98,8 +97,7 @@ type SyncingResponse struct { func NodeSyncingRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { status, err := getNodeStatus(cliCtx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 0fa06db3b821..abcef14522a7 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -184,8 +184,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } output, err := GetValidators(cliCtx, &height, page, limit) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } rest.PostProcessResponse(w, cliCtx, output) @@ -202,8 +201,7 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF } output, err := GetValidators(cliCtx, nil, page, limit) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/client/tx/tx.go b/client/tx/tx.go index 6d1c1cd08157..21fa4c16ffee 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -161,8 +161,7 @@ func WriteGeneratedTxResponse( } simAndExec, gas, err := flags.ParseGas(br.Gas) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -182,8 +181,7 @@ func WriteGeneratedTxResponse( } _, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -196,14 +194,12 @@ func WriteGeneratedTxResponse( } tx, err := BuildUnsignedTx(txf, msgs...) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } output, err := ctx.Marshaler.MarshalJSON(tx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/types/rest/rest.go b/types/rest/rest.go index 74bf5946d4c4..b0ef4f555da0 100644 --- a/types/rest/rest.go +++ b/types/rest/rest.go @@ -123,8 +123,7 @@ func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool { // Writes an error response to ResponseWriter and returns true if errors occurred. func ReadRESTReq(w http.ResponseWriter, r *http.Request, m codec.JSONMarshaler, req interface{}) bool { body, err := ioutil.ReadAll(r.Body) - if err != nil { - WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if CheckBadRequestError(w, err) { return false } @@ -148,6 +147,34 @@ func NewErrorResponse(code int, err string) ErrorResponse { return ErrorResponse{Code: code, Error: err} } +// CheckError takes care of writing an error response if err is not nil. +// Returns false when err is nil; it returns true otherwise. +func CheckError(w http.ResponseWriter, status int, err error) bool { + if err != nil { + WriteErrorResponse(w, status, err.Error()) + return true + } + return false +} + +// CheckBadRequestError attaches an error message to an HTTP 400 BAD REQUEST response. +// Returns false when err is nil; it returns true otherwise. +func CheckBadRequestError(w http.ResponseWriter, err error) bool { + return CheckError(w, http.StatusBadRequest, err) +} + +// CheckInternalServerError attaches an error message to an HTTP 500 INTERNAL SERVER ERROR response. +// Returns false when err is nil; it returns true otherwise. +func CheckInternalServerError(w http.ResponseWriter, err error) bool { + return CheckError(w, http.StatusInternalServerError, err) +} + +// CheckNotFoundError attaches an error message to an HTTP 404 NOT FOUND response. +// Returns false when err is nil; it returns true otherwise. +func CheckNotFoundError(w http.ResponseWriter, err error) bool { + return CheckError(w, http.StatusNotFound, err) +} + // WriteErrorResponse prepares and writes a HTTP error // given a status code and an error message. func WriteErrorResponse(w http.ResponseWriter, status int, err string) { @@ -162,8 +189,7 @@ func WriteSimulationResponse(w http.ResponseWriter, m codec.JSONMarshaler, gas u gasEst := GasEstimateResponse{GasEstimate: gas} resp, err := m.MarshalJSON(gasEst) - if err != nil { - WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if CheckInternalServerError(w, err) { return } @@ -194,8 +220,7 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm } n, err := strconv.ParseFloat(s, 64) - if err != nil { - WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if CheckBadRequestError(w, err) { return n, false } @@ -208,8 +233,7 @@ func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, cliCtx context.CL heightStr := r.FormValue("height") if heightStr != "" { height, err := strconv.ParseInt(heightStr, 10, 64) - if err != nil { - WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if CheckBadRequestError(w, err) { return cliCtx, false } @@ -257,8 +281,7 @@ func PostProcessResponseBare(w http.ResponseWriter, ctx context.CLIContext, body resp, err = codec.MarshalIndentFromJSON(resp) } - if err != nil { - WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if CheckInternalServerError(w, err) { return } } @@ -302,8 +325,7 @@ func PostProcessResponse(w http.ResponseWriter, ctx context.CLIContext, resp int result, err = codec.MarshalIndentFromJSON(result) } - if err != nil { - WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if CheckInternalServerError(w, err) { return } } @@ -315,8 +337,7 @@ func PostProcessResponse(w http.ResponseWriter, ctx context.CLIContext, resp int output, err = codec.MarshalIndentFromJSON(output) } - if err != nil { - WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if CheckInternalServerError(w, err) { return } diff --git a/types/rest/rest_test.go b/types/rest/rest_test.go index 0f259b7fc14c..189316a7662c 100644 --- a/types/rest/rest_test.go +++ b/types/rest/rest_test.go @@ -440,3 +440,35 @@ func mustNewRequest(t *testing.T, method, url string, body io.Reader) *http.Requ require.NoError(t, err) return req } + +func TestCheckErrors(t *testing.T) { + t.Parallel() + err := errors.New("ERROR") + tests := []struct { + name string + checkerFn func(w http.ResponseWriter, err error) bool + error error + wantErr bool + wantString string + wantStatus int + }{ + {"500", CheckInternalServerError, err, true, `{"error":"ERROR"}`, http.StatusInternalServerError}, + {"500 (no error)", CheckInternalServerError, nil, false, ``, http.StatusInternalServerError}, + {"400", CheckBadRequestError, err, true, `{"error":"ERROR"}`, http.StatusBadRequest}, + {"400 (no error)", CheckBadRequestError, nil, false, ``, http.StatusBadRequest}, + {"404", CheckNotFoundError, err, true, `{"error":"ERROR"}`, http.StatusNotFound}, + {"404 (no error)", CheckNotFoundError, nil, false, ``, http.StatusNotFound}, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + require.Equal(t, tt.wantErr, tt.checkerFn(w, tt.error)) + if tt.wantErr { + require.Equal(t, w.Body.String(), tt.wantString) + require.Equal(t, w.Code, tt.wantStatus) + } + }) + } +} diff --git a/x/auth/client/rest.go b/x/auth/client/rest.go index f79caa1af30e..aba689f75b19 100644 --- a/x/auth/client/rest.go +++ b/x/auth/client/rest.go @@ -19,8 +19,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext } simAndExec, gas, err := flags.ParseGas(br.Gas) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -36,8 +35,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext } txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -48,14 +46,12 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext } stdMsg, err := txBldr.BuildSignMsg(msgs) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } output, err := cliCtx.Codec.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/auth/client/rest/broadcast.go b/x/auth/client/rest/broadcast.go index 92a94eefccd5..5ce235ab1a61 100644 --- a/x/auth/client/rest/broadcast.go +++ b/x/auth/client/rest/broadcast.go @@ -23,28 +23,23 @@ func BroadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc { var req BroadcastReq body, err := ioutil.ReadAll(r.Body) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } - err = cliCtx.Codec.UnmarshalJSON(body, &req) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if err := cliCtx.Codec.UnmarshalJSON(body, &req); rest.CheckBadRequestError(w, err) { return } txBytes, err := cliCtx.Codec.MarshalBinaryBare(req.Tx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } cliCtx = cliCtx.WithBroadcastMode(req.Mode) res, err := cliCtx.BroadcastTx(txBytes) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/auth/client/rest/decode.go b/x/auth/client/rest/decode.go index dfe7436b8e72..2a71e83ff865 100644 --- a/x/auth/client/rest/decode.go +++ b/x/auth/client/rest/decode.go @@ -28,27 +28,23 @@ func DecodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { var req DecodeReq body, err := ioutil.ReadAll(r.Body) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } err = cliCtx.Codec.UnmarshalJSON(body, &req) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } txBytes, err := base64.StdEncoding.DecodeString(req.Tx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } var stdTx authtypes.StdTx err = cliCtx.Codec.UnmarshalBinaryBare(txBytes, &stdTx) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } diff --git a/x/auth/client/rest/encode.go b/x/auth/client/rest/encode.go index b2fe6681e10e..1ffebeab6066 100644 --- a/x/auth/client/rest/encode.go +++ b/x/auth/client/rest/encode.go @@ -23,21 +23,18 @@ func EncodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { var req types.StdTx body, err := ioutil.ReadAll(r.Body) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } err = cliCtx.Codec.UnmarshalJSON(body, &req) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } // re-encode it via the Amino wire protocol txBytes, err := cliCtx.Codec.MarshalBinaryBare(req) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index 8ff7f567d05b..a350bed55543 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -23,8 +23,7 @@ func QueryAccountRequestHandlerFn(storeName string, cliCtx context.CLIContext) h bech32addr := vars["address"] addr, err := sdk.AccAddressFromBech32(bech32addr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -94,14 +93,12 @@ func QueryTxsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } events, page, limit, err = rest.ParseHTTPArgs(r) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } searchResult, err := client.QueryTxsByEvents(cliCtx, events, page, limit, "") - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -148,8 +145,7 @@ func queryParamsHandler(cliCtx context.CLIContext) http.HandlerFunc { route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/bank/client/rest/query.go b/x/bank/client/rest/query.go index baf1cbe42922..1b1a3bab4058 100644 --- a/x/bank/client/rest/query.go +++ b/x/bank/client/rest/query.go @@ -22,8 +22,7 @@ func QueryBalancesRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc { bech32addr := vars["address"] addr, err := sdk.AccAddressFromBech32(bech32addr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -57,14 +56,12 @@ func QueryBalancesRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc { } bz, err := marshaler.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := ctx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/bank/client/rest/tx.go b/x/bank/client/rest/tx.go index b9712c160acf..db671b741b4f 100644 --- a/x/bank/client/rest/tx.go +++ b/x/bank/client/rest/tx.go @@ -30,8 +30,7 @@ func NewSendRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg tx.G bech32Addr := vars["address"] toAddr, err := sdk.AccAddressFromBech32(bech32Addr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -46,8 +45,7 @@ func NewSendRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg tx.G } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -72,8 +70,7 @@ func SendRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { bech32Addr := vars["address"] toAddr, err := sdk.AccAddressFromBech32(bech32Addr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -88,8 +85,7 @@ func SendRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } diff --git a/x/distribution/client/rest/query.go b/x/distribution/client/rest/query.go index 4c362409dd13..1d614c17fe6a 100644 --- a/x/distribution/client/rest/query.go +++ b/x/distribution/client/rest/query.go @@ -87,8 +87,7 @@ func delegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) htt route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -134,8 +133,7 @@ func delegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, queryRoute stri bz := cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr)) res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", queryRoute), bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -177,14 +175,12 @@ func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.H // query commission bz, err := common.QueryValidatorCommission(cliCtx, queryRoute, valAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var commission types.ValidatorAccumulatedCommission - if err := cliCtx.Codec.UnmarshalJSON(bz, &commission); err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, cliCtx.Codec.UnmarshalJSON(bz, &commission)) { return } @@ -196,14 +192,12 @@ func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.H } var rewards sdk.DecCoins - if err := cliCtx.Codec.UnmarshalJSON(bz, &rewards); err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, cliCtx.Codec.UnmarshalJSON(bz, &rewards)) { return } bz, err = cliCtx.Codec.MarshalJSON(NewValidatorDistInfo(delAddr, rewards, commission)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -247,8 +241,7 @@ func paramsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerF route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams) res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -265,14 +258,12 @@ func communityPoolHandler(cliCtx context.CLIContext, queryRoute string) http.Han } res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var result sdk.DecCoins - if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, cliCtx.Codec.UnmarshalJSON(res, &result)) { return } @@ -296,8 +287,7 @@ func outstandingRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) h bin := cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr)) res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -311,8 +301,7 @@ func checkResponseQueryDelegationRewards( ) (res []byte, height int64, ok bool) { res, height, err := common.QueryDelegationRewards(cliCtx, queryRoute, delAddr, valAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return nil, 0, false } diff --git a/x/distribution/client/rest/rest.go b/x/distribution/client/rest/rest.go index 1a19f5b633a1..ee26ce9544f5 100644 --- a/x/distribution/client/rest/rest.go +++ b/x/distribution/client/rest/rest.go @@ -43,8 +43,7 @@ func postProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { content := types.NewCommunityPoolSpendProposal(req.Title, req.Description, req.Recipient, req.Amount) msg := gov.NewMsgSubmitProposal(content, req.Deposit, req.Proposer) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } diff --git a/x/distribution/client/rest/tx.go b/x/distribution/client/rest/tx.go index 23713add7efd..233c92832c78 100644 --- a/x/distribution/client/rest/tx.go +++ b/x/distribution/client/rest/tx.go @@ -83,8 +83,7 @@ func withdrawDelegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute str } msgs, err := common.WithdrawAllDelegatorRewards(cliCtx, queryRoute, delAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -118,8 +117,7 @@ func withdrawDelegationRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerF } msg := types.NewMsgWithdrawDelegatorReward(delAddr, valAddr) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -148,8 +146,7 @@ func setDelegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext) http.Handler } msg := types.NewMsgSetWithdrawAddress(delAddr, req.WithdrawAddress) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -179,8 +176,7 @@ func withdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFu // prepare multi-message transaction msgs, err := common.WithdrawValidatorRewardsAndCommission(valAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -201,14 +197,12 @@ func fundCommunityPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } msg := types.NewMsgFundCommunityPool(req.Amount, fromAddr) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -220,8 +214,7 @@ func fundCommunityPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) { addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["delegatorAddr"]) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return nil, false } @@ -230,8 +223,7 @@ func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAd func checkValidatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.ValAddress, bool) { addr, err := sdk.ValAddressFromBech32(mux.Vars(r)["validatorAddr"]) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return nil, false } diff --git a/x/evidence/client/rest/query.go b/x/evidence/client/rest/query.go index 24fc811eb5e0..071693b09c1a 100644 --- a/x/evidence/client/rest/query.go +++ b/x/evidence/client/rest/query.go @@ -53,8 +53,7 @@ func queryEvidenceHandler(cliCtx context.CLIContext) http.HandlerFunc { route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryEvidence) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -66,8 +65,7 @@ func queryEvidenceHandler(cliCtx context.CLIContext) http.HandlerFunc { func queryAllEvidenceHandler(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -85,8 +83,7 @@ func queryAllEvidenceHandler(cliCtx context.CLIContext) http.HandlerFunc { route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllEvidence) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -104,8 +101,7 @@ func queryParamsHandler(cliCtx context.CLIContext) http.HandlerFunc { route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters) res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/gov/client/rest/query.go b/x/gov/client/rest/query.go index 3eb5800d7a99..4b7f42bfa5e0 100644 --- a/x/gov/client/rest/query.go +++ b/x/gov/client/rest/query.go @@ -37,8 +37,7 @@ func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/gov/%s/%s", types.QueryParams, paramType), nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusNotFound, err.Error()) + if rest.CheckNotFoundError(w, err) { return } @@ -71,14 +70,12 @@ func queryProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryProposalParams(proposalID) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData("custom/gov/proposal", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -105,20 +102,17 @@ func queryDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryProposalParams(proposalID) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, _, err := cliCtx.QueryWithData("custom/gov/proposal", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var proposal types.Proposal - if err := cliCtx.Codec.UnmarshalJSON(res, &proposal); err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, cliCtx.Codec.UnmarshalJSON(res, &proposal)) { return } @@ -131,8 +125,7 @@ func queryDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { res, _, err = cliCtx.QueryWithData("custom/gov/deposits", bz) } - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -156,8 +149,7 @@ func queryProposerHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, err := gcutils.QueryProposerByTxQuery(cliCtx, proposalID) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -189,8 +181,7 @@ func queryDepositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -202,20 +193,17 @@ func queryDepositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryDepositParams(proposalID, depositorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, _, err := cliCtx.QueryWithData("custom/gov/deposit", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var deposit types.Deposit - if err := cliCtx.Codec.UnmarshalJSON(res, &deposit); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, cliCtx.Codec.UnmarshalJSON(res, &deposit)) { return } @@ -224,8 +212,7 @@ func queryDepositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { // for directly via a txs query. if deposit.Empty() { bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryProposalParams(proposalID)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -237,8 +224,7 @@ func queryDepositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, err = gcutils.QueryDepositByTxQuery(cliCtx, params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } } @@ -284,20 +270,17 @@ func queryVoteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryVoteParams(proposalID, voterAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, _, err := cliCtx.QueryWithData("custom/gov/vote", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var vote types.Vote - if err := cliCtx.Codec.UnmarshalJSON(res, &vote); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, cliCtx.Codec.UnmarshalJSON(res, &vote)) { return } @@ -306,8 +289,7 @@ func queryVoteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { // directly via a txs query. if vote.Empty() { bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryProposalParams(proposalID)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -319,8 +301,7 @@ func queryVoteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, err = gcutils.QueryVoteByTxQuery(cliCtx, params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } } @@ -333,8 +314,7 @@ func queryVoteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -360,20 +340,17 @@ func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryProposalVotesParams(proposalID, page, limit) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, _, err := cliCtx.QueryWithData("custom/gov/proposal", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } var proposal types.Proposal - if err := cliCtx.Codec.UnmarshalJSON(res, &proposal); err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, cliCtx.Codec.UnmarshalJSON(res, &proposal)) { return } @@ -386,8 +363,7 @@ func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { res, _, err = cliCtx.QueryWithData("custom/gov/votes", bz) } - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -399,8 +375,7 @@ func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func queryProposalsWithParameterFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -417,39 +392,34 @@ func queryProposalsWithParameterFn(cliCtx context.CLIContext) http.HandlerFunc { if v := r.URL.Query().Get(RestVoter); len(v) != 0 { voterAddr, err = sdk.AccAddressFromBech32(v) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } } if v := r.URL.Query().Get(RestDepositor); len(v) != 0 { depositorAddr, err = sdk.AccAddressFromBech32(v) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } } if v := r.URL.Query().Get(RestProposalStatus); len(v) != 0 { proposalStatus, err = types.ProposalStatusFromString(gcutils.NormalizeProposalStatus(v)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } } params := types.NewQueryProposalsParams(page, limit, proposalStatus, voterAddr, depositorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryProposals) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -483,14 +453,12 @@ func queryTallyOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryProposalParams(proposalID) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData("custom/gov/tally", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/gov/client/rest/tx.go b/x/gov/client/rest/tx.go index fb2bfa5c6420..627525a49a24 100644 --- a/x/gov/client/rest/tx.go +++ b/x/gov/client/rest/tx.go @@ -41,8 +41,7 @@ func postProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { content := types.ContentFromProposalType(req.Title, req.Description, proposalType) msg := types.NewMsgSubmitProposal(content, req.InitialDeposit, req.Proposer) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -77,8 +76,7 @@ func depositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { // create the message msg := types.NewMsgDeposit(req.Depositor, proposalID, req.Amount) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -112,15 +110,13 @@ func voteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } voteOption, err := types.VoteOptionFromString(gcutils.NormalizeVoteOption(req.Option)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } // create the message msg := types.NewMsgVote(req.Voter, proposalID, voteOption) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } diff --git a/x/mint/client/rest/query.go b/x/mint/client/rest/query.go index f9fc7d43b167..7cfb69775fc5 100644 --- a/x/mint/client/rest/query.go +++ b/x/mint/client/rest/query.go @@ -38,8 +38,7 @@ func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -58,8 +57,7 @@ func queryInflationHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -78,8 +76,7 @@ func queryAnnualProvisionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc } res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/params/client/rest/rest.go b/x/params/client/rest/rest.go index 665dea834f05..59c5a4cd3df3 100644 --- a/x/params/client/rest/rest.go +++ b/x/params/client/rest/rest.go @@ -37,8 +37,7 @@ func postProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { content := proposal.NewParameterChangeProposal(req.Title, req.Description, req.Changes.ToParamChanges()) msg := govtypes.NewMsgSubmitProposal(content, req.Deposit, req.Proposer) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index 2d3d9bfab798..f82ec97afedb 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -34,8 +34,7 @@ func signingInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, vars["validatorPubKey"]) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -47,15 +46,13 @@ func signingInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address())) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfo) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -68,8 +65,7 @@ func signingInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func signingInfoHandlerListFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -80,15 +76,13 @@ func signingInfoHandlerListFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQuerySigningInfosParams(page, limit) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfos) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -107,8 +101,7 @@ func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute) res, height, err := cliCtx.QueryWithData(route, nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/slashing/client/rest/tx.go b/x/slashing/client/rest/tx.go index f0ce72c6ae6d..dba316db8462 100644 --- a/x/slashing/client/rest/tx.go +++ b/x/slashing/client/rest/tx.go @@ -43,14 +43,12 @@ func NewUnjailRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg tx } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } valAddr, err := sdk.ValAddressFromBech32(bech32Validator) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -60,9 +58,7 @@ func NewUnjailRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg tx } msg := types.NewMsgUnjail(valAddr) - err = msg.ValidateBasic() - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } tx.WriteGeneratedTxResponse(ctx, w, txg, req.BaseReq, msg) @@ -99,14 +95,12 @@ func unjailRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } valAddr, err := sdk.ValAddressFromBech32(bech32validator) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -116,9 +110,7 @@ func unjailRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } msg := types.NewMsgUnjail(valAddr) - err = msg.ValidateBasic() - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } diff --git a/x/staking/client/rest/query.go b/x/staking/client/rest/query.go index ddd6c03c7c6b..5f307e77695c 100644 --- a/x/staking/client/rest/query.go +++ b/x/staking/client/rest/query.go @@ -125,8 +125,7 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { delegatorAddr := vars["delegatorAddr"] _, err := sdk.AccAddressFromBech32(delegatorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -173,15 +172,14 @@ func delegatorTxsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { for _, action := range actions { foundTxs, errQuery := queryTxs(cliCtx, action, delegatorAddr) - if errQuery != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, errQuery.Error()) + if rest.CheckInternalServerError(w, errQuery) { + return } txs = append(txs, foundTxs) } res, err := cliCtx.Codec.MarshalJSON(txs) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -210,8 +208,7 @@ func redelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { if len(bechDelegatorAddr) != 0 { delegatorAddr, err := sdk.AccAddressFromBech32(bechDelegatorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } params.DelegatorAddr = delegatorAddr @@ -219,8 +216,7 @@ func redelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { if len(bechSrcValidatorAddr) != 0 { srcValidatorAddr, err := sdk.ValAddressFromBech32(bechSrcValidatorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } params.SrcValidatorAddr = srcValidatorAddr @@ -228,22 +224,19 @@ func redelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { if len(bechDstValidatorAddr) != 0 { dstValidatorAddr, err := sdk.ValAddressFromBech32(bechDstValidatorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } params.DstValidatorAddr = dstValidatorAddr } bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData("custom/staking/redelegations", bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -271,8 +264,7 @@ func delegatorValidatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func validatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -288,15 +280,13 @@ func validatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryValidatorsParams(page, limit, status) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidators) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -333,15 +323,13 @@ func historicalInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryHistoricalInfoParams(height) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryHistoricalInfo) res, height, err := cliCtx.QueryWithData(route, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -359,8 +347,7 @@ func poolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, height, err := cliCtx.QueryWithData("custom/staking/pool", nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -378,8 +365,7 @@ func paramsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } res, height, err := cliCtx.QueryWithData("custom/staking/parameters", nil) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/staking/client/rest/tx.go b/x/staking/client/rest/tx.go index 7838d95515cd..402d52a08255 100644 --- a/x/staking/client/rest/tx.go +++ b/x/staking/client/rest/tx.go @@ -69,14 +69,12 @@ func postDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -103,14 +101,12 @@ func postRedelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -137,14 +133,12 @@ func postUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFu } msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } diff --git a/x/staking/client/rest/utils.go b/x/staking/client/rest/utils.go index 80e0a54be1a4..1cfaa4b4f02f 100644 --- a/x/staking/client/rest/utils.go +++ b/x/staking/client/rest/utils.go @@ -42,14 +42,12 @@ func queryBonds(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { bech32validator := vars["validatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } validatorAddr, err := sdk.ValAddressFromBech32(bech32validator) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -61,14 +59,12 @@ func queryBonds(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { params := types.NewQueryBondsParams(delegatorAddr, validatorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData(endpoint, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -83,8 +79,7 @@ func queryDelegator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc bech32delegator := vars["delegatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -96,14 +91,12 @@ func queryDelegator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc params := types.NewQueryDelegatorParams(delegatorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData(endpoint, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -118,8 +111,7 @@ func queryValidator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc bech32validatorAddr := vars["validatorAddr"] validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -131,14 +123,12 @@ func queryValidator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc params := types.NewQueryValidatorParams(validatorAddr) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData(endpoint, bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/supply/client/rest/query.go b/x/supply/client/rest/query.go index 021159dfaf17..fd5256b6b9e7 100644 --- a/x/supply/client/rest/query.go +++ b/x/supply/client/rest/query.go @@ -34,8 +34,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { func totalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } @@ -46,14 +45,12 @@ func totalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQueryTotalSupplyParams(page, limit) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -73,14 +70,12 @@ func supplyOfHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { params := types.NewQuerySupplyOfParams(denom) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/upgrade/client/rest/query.go b/x/upgrade/client/rest/query.go index 8c3372b2a7aa..101235419b2c 100644 --- a/x/upgrade/client/rest/query.go +++ b/x/upgrade/client/rest/query.go @@ -23,8 +23,7 @@ func getCurrentPlanHandler(cliCtx context.CLIContext) func(http.ResponseWriter, return func(w http.ResponseWriter, request *http.Request) { // ignore height for now res, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryCurrent)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } if len(res) == 0 { @@ -34,8 +33,7 @@ func getCurrentPlanHandler(cliCtx context.CLIContext) func(http.ResponseWriter, var plan types.Plan err = cliCtx.Codec.UnmarshalBinaryBare(res, &plan) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -49,14 +47,12 @@ func getDonePlanHandler(cliCtx context.CLIContext) func(http.ResponseWriter, *ht params := types.NewQueryAppliedParams(name) bz, err := cliCtx.Codec.MarshalJSON(params) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryApplied), bz) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } diff --git a/x/upgrade/client/rest/tx.go b/x/upgrade/client/rest/tx.go index e67aa79bfc61..6b40ae202892 100644 --- a/x/upgrade/client/rest/tx.go +++ b/x/upgrade/client/rest/tx.go @@ -62,16 +62,14 @@ func postPlanHandler(cliCtx context.CLIContext) http.HandlerFunc { } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } var t time.Time if req.UpgradeTime != "" { t, err = time.Parse(time.RFC3339, req.UpgradeTime) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } } @@ -79,8 +77,7 @@ func postPlanHandler(cliCtx context.CLIContext) http.HandlerFunc { plan := types.Plan{Name: req.UpgradeName, Time: t, Height: req.UpgradeHeight, Info: req.UpgradeInfo} content := types.NewSoftwareUpgradeProposal(req.Title, req.Description, plan) msg := gov.NewMsgSubmitProposal(content, req.Deposit, fromAddr) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return } @@ -102,15 +99,13 @@ func cancelPlanHandler(cliCtx context.CLIContext) http.HandlerFunc { } fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } content := types.NewCancelSoftwareUpgradeProposal(req.Title, req.Description) msg := gov.NewMsgSubmitProposal(content, req.Deposit, fromAddr) - if err := msg.ValidateBasic(); err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, msg.ValidateBasic()) { return }