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

Merge PR optimize status code for wasm query #161

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
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
74 changes: 62 additions & 12 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func listCodesHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
queryClient := types.NewQueryClient(cliCtx)
pageReq, err := rest.ParseGRPCWasmPageRequest(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var reverse bool
Expand All @@ -124,7 +124,7 @@ func listCodesHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
} else {
reverse, err = strconv.ParseBool(reverseStr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
}
Expand Down Expand Up @@ -171,12 +171,17 @@ func queryCodeHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "code not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if res == nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "contract not found")
rest.WriteErrorResponse(w, http.StatusNotFound, "code not found")
return
}

Expand All @@ -193,7 +198,7 @@ func listContractsByCodeHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
codeID, err := strconv.ParseUint(mux.Vars(r)["codeID"], 10, 64)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand All @@ -204,7 +209,7 @@ func listContractsByCodeHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc
queryClient := types.NewQueryClient(cliCtx)
pageReq, err := rest.ParseGRPCWasmPageRequest(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var reverse bool
Expand Down Expand Up @@ -256,6 +261,12 @@ func queryContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
Address: mux.Vars(r)["contractAddr"],
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "contract not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -283,6 +294,12 @@ func queryCodeContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
Address: mux.Vars(r)["contractAddr"],
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "contract not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -297,6 +314,11 @@ func queryCodeContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "code not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -319,7 +341,7 @@ func queryContractBlockedMethodsHandlerFn(cliCtx clientCtx.CLIContext) http.Hand
return func(w http.ResponseWriter, r *http.Request) {
addr, err := sdk.WasmAddressFromBech32(mux.Vars(r)["contractAddr"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand All @@ -329,6 +351,12 @@ func queryContractBlockedMethodsHandlerFn(cliCtx clientCtx.CLIContext) http.Hand

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryListContractBlockedMethod, addr.String())
res, height, err := cliCtx.Query(route)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "methods not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -351,7 +379,7 @@ func queryContractStateAllHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFun
queryClient := types.NewQueryClient(cliCtx)
pageReq, err := rest.ParseGRPCWasmPageRequest(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var reverse bool
Expand All @@ -361,7 +389,7 @@ func queryContractStateAllHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFun
} else {
reverse, err = strconv.ParseBool(reverseStr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
}
Expand All @@ -380,6 +408,11 @@ func queryContractStateAllHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFun
}
}

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "state not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -395,7 +428,7 @@ func queryContractStateRawHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFun
decoder.encoding = mux.Vars(r)["encoding"]
queryData, err := decoder.DecodeString(mux.Vars(r)["key"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand All @@ -411,6 +444,11 @@ func queryContractStateRawHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFun
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "state not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -435,7 +473,7 @@ func queryContractStateSmartHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerF
decoder.encoding = mux.Vars(r)["encoding"]
queryData, err := decoder.DecodeString(mux.Vars(r)["query"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

Expand All @@ -448,6 +486,11 @@ func queryContractStateSmartHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerF
},
)

if isErrNotFound(err) {
rest.WriteErrorResponse(w, http.StatusNotFound, "state not found")
return
}

if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -474,7 +517,7 @@ func queryContractHistoryFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
queryClient := types.NewQueryClient(cliCtx)
pageReq, err := rest.ParseGRPCWasmPageRequest(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
var reverse bool
Expand All @@ -484,7 +527,7 @@ func queryContractHistoryFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
} else {
reverse, err = strconv.ParseBool(reverseStr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
}
Expand Down Expand Up @@ -532,3 +575,10 @@ func (a *argumentDecoder) DecodeString(s string) ([]byte, error) {
return a.dec(s)
}
}

func isErrNotFound(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "not found")
}