Skip to content

Commit 8b9c203

Browse files
author
Jason Yellick
committed
FAB-10047 Report error on list instantiated
Presently, the 'peer chaincode list --instantiated' command silently ignores a request which fails, and simply prints no instantiated chaincodes. This CR patches the peer CLI to check for an error response and to return an error. Change-Id: I222c2131efda35e768bece3e08758f55bdb7b4f8 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 99f788d commit 8b9c203

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

peer/chaincode/list.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ package chaincode
99
import (
1010
"bytes"
1111
"encoding/hex"
12-
"errors"
1312
"fmt"
1413
"reflect"
1514

1615
"github.com/golang/protobuf/proto"
16+
cb "github.com/hyperledger/fabric/protos/common"
1717
pb "github.com/hyperledger/fabric/protos/peer"
1818
"github.com/hyperledger/fabric/protos/utils"
19+
"github.com/pkg/errors"
1920
"github.com/spf13/cobra"
2021
"golang.org/x/net/context"
2122
)
@@ -92,7 +93,15 @@ func getChaincodes(cmd *cobra.Command, cf *ChaincodeCmdFactory) error {
9293
// list is currently only supported for one peer
9394
proposalResponse, err := cf.EndorserClients[0].ProcessProposal(context.Background(), signedProp)
9495
if err != nil {
95-
return fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
96+
return errors.Errorf("Error endorsing %s: %s", chainFuncName, err)
97+
}
98+
99+
if proposalResponse.Response == nil {
100+
return errors.Errorf("Proposal response had nil 'response'")
101+
}
102+
103+
if proposalResponse.Response.Status != int32(cb.Status_SUCCESS) {
104+
return errors.Errorf("Bad response: %d - %s", proposalResponse.Response.Status, proposalResponse.Response.Message)
96105
}
97106

98107
cqr := &pb.ChaincodeQueryResponse{}

peer/chaincode/list_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ func TestChaincodeListCmd(t *testing.T) {
106106
}
107107
}
108108

109+
func TestChaincodeListFailure(t *testing.T) {
110+
InitMSP()
111+
112+
signer, err := common.GetDefaultSigner()
113+
if err != nil {
114+
t.Fatalf("Get default signer error: %s", err)
115+
}
116+
117+
mockResponse := &pb.ProposalResponse{
118+
Response: &pb.Response{Status: 500, Message: "error message"},
119+
Endorsement: &pb.Endorsement{},
120+
}
121+
mockEndorserClients := []pb.EndorserClient{common.GetMockEndorserClient(mockResponse, nil)}
122+
mockBroadcastClient := common.GetMockBroadcastClient(nil)
123+
mockCF := &ChaincodeCmdFactory{
124+
EndorserClients: mockEndorserClients,
125+
Signer: signer,
126+
BroadcastClient: mockBroadcastClient,
127+
}
128+
129+
// reset channelID, it might have been set by previous test
130+
channelID = ""
131+
132+
resetFlags()
133+
134+
// Get instantiated chaincodes
135+
instantiatedChaincodesCmd := listCmd(mockCF)
136+
args := []string{"--instantiated", "-C", "mychannel"}
137+
instantiatedChaincodesCmd.SetArgs(args)
138+
err = instantiatedChaincodesCmd.Execute()
139+
assert.Error(t, err)
140+
assert.Regexp(t, "Bad response: 500 - error message", err.Error())
141+
}
142+
109143
func TestString(t *testing.T) {
110144
id := []byte{1, 2, 3, 4, 5}
111145
idBytes := hex.EncodeToString(id)

0 commit comments

Comments
 (0)