From 4cb14618d9d6e69f108cff89e9f652db082997b9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 25 Jan 2022 17:06:37 +0800 Subject: [PATCH] fix: return 404 on non-existing tx (backport #10992) (#11014) * fix: return 404 on non-existing tx (#10992) (cherry picked from commit 158128953c45f95fb4fd7ed737005b2bd9c9f1eb) # Conflicts: # CHANGELOG.md * fix cl Co-authored-by: Aleksandr Bezobchuk Co-authored-by: Aleksandr Bezobchuk --- x/auth/tx/service.go | 8 ++++++++ x/auth/tx/service_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index 1966a201847c..c0248d7f58a7 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -131,10 +131,18 @@ func (s txServer) GetTx(ctx context.Context, req *txtypes.GetTxRequest) (*txtype return nil, status.Error(codes.InvalidArgument, "request cannot be nil") } + if len(req.Hash) == 0 { + return nil, status.Error(codes.InvalidArgument, "tx hash cannot be empty") + } + // TODO We should also check the proof flag in gRPC header. // https://github.com/cosmos/cosmos-sdk/issues/7036. result, err := queryTx(ctx, s.clientCtx, req.Hash) if err != nil { + if strings.Contains(err.Error(), "not found") { + return nil, status.Errorf(codes.NotFound, "tx not found: %s", req.Hash) + } + return nil, err } diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index 4c13e8354df1..e82baca41534 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -328,8 +328,8 @@ func (s IntegrationTestSuite) TestGetTx_GRPC() { expErrMsg string }{ {"nil request", nil, true, "request cannot be nil"}, - {"empty request", &tx.GetTxRequest{}, true, "transaction hash cannot be empty"}, - {"request with dummy hash", &tx.GetTxRequest{Hash: "deadbeef"}, true, "tx (DEADBEEF) not found"}, + {"empty request", &tx.GetTxRequest{}, true, "tx hash cannot be empty"}, + {"request with dummy hash", &tx.GetTxRequest{Hash: "deadbeef"}, true, "code = NotFound desc = tx not found: deadbeef"}, {"good request", &tx.GetTxRequest{Hash: s.txRes.TxHash}, false, ""}, } for _, tc := range testCases { @@ -358,12 +358,12 @@ func (s IntegrationTestSuite) TestGetTx_GRPCGateway() { { "empty params", fmt.Sprintf("%s/cosmos/tx/v1beta1/txs/", val.APIAddress), - true, "transaction hash cannot be empty", + true, "tx hash cannot be empty", }, { "dummy hash", fmt.Sprintf("%s/cosmos/tx/v1beta1/txs/%s", val.APIAddress, "deadbeef"), - true, "tx (DEADBEEF) not found", + true, "code = NotFound desc = tx not found: deadbeef", }, { "good hash",