Skip to content

Commit 5db3e48

Browse files
committed
Cleanup messaging and error formating, peer cli
Peer cli chaincode command code has a few formatting and logging warning and inconsistences, this commit clean this issues. Change-Id: Iaf31daa3fcc63ca6b909ffbdf046e687fa7c16e4 Signed-off-by: Artem Barger <[email protected]>
1 parent a0d032b commit 5db3e48

9 files changed

+48
-45
lines changed

peer/chaincode/common.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool, cf *
106106
logger.Infof("Invoke result: %v", proposalResp)
107107
} else {
108108
if proposalResp == nil {
109-
return fmt.Errorf("Error query %s by endorsing: %s\n", chainFuncName, err)
109+
return fmt.Errorf("Error query %s by endorsing: %s", chainFuncName, err)
110110
}
111111

112112
if chaincodeQueryRaw {
113113
if chaincodeQueryHex {
114-
err = errors.New("Options --raw (-r) and --hex (-x) are not compatible\n")
114+
err = errors.New("Options --raw (-r) and --hex (-x) are not compatible")
115115
return
116116
}
117117
fmt.Print("Query Result (Raw): ")
@@ -130,7 +130,7 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool, cf *
130130
func checkChaincodeCmdParams(cmd *cobra.Command) error {
131131
//we need chaincode name for everything, including deploy
132132
if chaincodeName == common.UndefinedParamValue {
133-
return fmt.Errorf("Must supply value for %s name parameter.\n", chainFuncName)
133+
return fmt.Errorf("Must supply value for %s name parameter.", chainFuncName)
134134
}
135135

136136
if cmd.Name() == instantiate_cmdname || cmd.Name() == install_cmdname || cmd.Name() == upgrade_cmdname {
@@ -142,35 +142,35 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
142142
// if it's not a deploy or an upgrade we don't need policy, escc and vscc
143143
if cmd.Name() != instantiate_cmdname && cmd.Name() != upgrade_cmdname {
144144
if escc != common.UndefinedParamValue {
145-
return fmt.Errorf("escc should be supplied only to chaincode deploy requests")
145+
return errors.New("escc should be supplied only to chaincode deploy requests")
146146
}
147147

148148
if vscc != common.UndefinedParamValue {
149-
return fmt.Errorf("vscc should be supplied only to chaincode deploy requests")
149+
return errors.New("vscc should be supplied only to chaincode deploy requests")
150150
}
151151

152152
if policy != common.UndefinedParamValue {
153-
return fmt.Errorf("policy should be supplied only to chaincode deploy requests")
153+
return errors.New("policy should be supplied only to chaincode deploy requests")
154154
}
155155
} else {
156156
if escc != common.UndefinedParamValue {
157157
logger.Infof("Using escc %s", escc)
158158
} else {
159-
logger.Infof("Using default escc")
159+
logger.Info("Using default escc")
160160
escc = "escc"
161161
}
162162

163163
if vscc != common.UndefinedParamValue {
164164
logger.Infof("Using vscc %s", vscc)
165165
} else {
166-
logger.Infof("Using default vscc")
166+
logger.Info("Using default vscc")
167167
vscc = "vscc"
168168
}
169169

170170
if policy != common.UndefinedParamValue {
171171
p, err := cauthdsl.FromString(policy)
172172
if err != nil {
173-
return fmt.Errorf("Invalid policy %s\n", policy)
173+
return fmt.Errorf("Invalid policy %s", policy)
174174
}
175175
policyMarhsalled = putils.MarshalOrPanic(p)
176176
} else {
@@ -199,7 +199,7 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
199199
_, argsPresent := sm["args"]
200200
_, funcPresent := sm["function"]
201201
if !argsPresent || (len(m) == 2 && !funcPresent) || len(m) > 2 {
202-
return fmt.Errorf("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'")
202+
return errors.New("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'")
203203
}
204204
} else {
205205
if cmd == nil || cmd != chaincodeInstallCmd {

peer/chaincode/install.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ var chaincodeInstallCmd *cobra.Command
3333

3434
const install_cmdname = "install"
3535

36+
const install_desc = "Package the specified chaincode into a deployment spec and save it on the peer's path."
37+
3638
// installCmd returns the cobra command for Chaincode Deploy
3739
func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
3840
chaincodeInstallCmd = &cobra.Command{
3941
Use: "install",
40-
Short: fmt.Sprintf("Package the specified chaincode into a deployment spec and save it on the peer's path."),
41-
Long: fmt.Sprintf(`Package the specified chaincode into a deployment spec and save it on the peer's path.`),
42+
Short: fmt.Sprintf(install_desc),
43+
Long: fmt.Sprintf(install_desc),
4244
ValidArgs: []string{"1"},
4345
RunE: func(cmd *cobra.Command, args []string) error {
4446
return chaincodeInstall(cmd, args, cf)
@@ -52,27 +54,27 @@ func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
5254
func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDeploymentSpec, cf *ChaincodeCmdFactory) error {
5355
creator, err := cf.Signer.Serialize()
5456
if err != nil {
55-
return fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err)
57+
return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
5658
}
5759

5860
prop, _, err := utils.CreateInstallProposalFromCDS(cds, creator)
5961
if err != nil {
60-
return fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
62+
return fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
6163
}
6264

6365
var signedProp *pb.SignedProposal
6466
signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
6567
if err != nil {
66-
return fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err)
68+
return fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err)
6769
}
6870

6971
proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
7072
if err != nil {
71-
return fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err)
73+
return fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
7274
}
7375

7476
if proposalResponse != nil {
75-
fmt.Printf("Installed remotely %v\n", proposalResponse)
77+
logger.Debug("Installed remotely %v", proposalResponse)
7678
}
7779

7880
return nil
@@ -81,7 +83,7 @@ func install(chaincodeName string, chaincodeVersion string, cds *pb.ChaincodeDep
8183
// chaincodeInstall installs the chaincode. If remoteinstall, does it via a lccc call
8284
func chaincodeInstall(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error {
8385
if chaincodePath == common.UndefinedParamValue || chaincodeVersion == common.UndefinedParamValue {
84-
return fmt.Errorf("Must supply value for %s path and version parameters.\n", chainFuncName)
86+
return fmt.Errorf("Must supply value for %s path and version parameters.", chainFuncName)
8587
}
8688

8789
var err error

peer/chaincode/install_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestBadVersion(t *testing.T) {
6969
cmd.SetArgs(args)
7070

7171
if err := cmd.Execute(); err == nil {
72-
t.Fatalf("Expected error executing install command for version not specified")
72+
t.Fatal("Expected error executing install command for version not specified")
7373
}
7474
}
7575

@@ -84,11 +84,11 @@ func TestNonExistentCC(t *testing.T) {
8484
cmd.SetArgs(args)
8585

8686
if err := cmd.Execute(); err == nil {
87-
t.Fatalf("Expected error executing install command for bad chaincode")
87+
t.Fatal("Expected error executing install command for bad chaincode")
8888
}
8989

9090
if _, err := os.Stat(fsPath + "/chaincodes/badexample02.testversion"); err == nil {
91-
t.Fatalf("chaincode example02.testversion should not exist")
91+
t.Fatal("chaincode example02.testversion should not exist")
9292
}
9393
}
9494

peer/chaincode/instantiate.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ package chaincode
1919
import (
2020
"fmt"
2121

22-
"golang.org/x/net/context"
23-
2422
protcommon "github.com/hyperledger/fabric/protos/common"
2523
pb "github.com/hyperledger/fabric/protos/peer"
2624
"github.com/hyperledger/fabric/protos/utils"
2725
"github.com/spf13/cobra"
26+
"golang.org/x/net/context"
2827
)
2928

3029
var chaincodeInstantiateCmd *cobra.Command
3130

3231
const instantiate_cmdname = "instantiate"
3332

33+
const instantiate_desc = "Deploy the specified chaincode to the network."
34+
3435
// instantiateCmd returns the cobra command for Chaincode Deploy
3536
func instantiateCmd(cf *ChaincodeCmdFactory) *cobra.Command {
3637
chaincodeInstantiateCmd = &cobra.Command{
3738
Use: instantiate_cmdname,
38-
Short: fmt.Sprintf("Deploy the specified chaincode to the network."),
39-
Long: fmt.Sprintf(`Deploy the specified chaincode to the network.`),
39+
Short: fmt.Sprintf(instantiate_desc),
40+
Long: fmt.Sprintf(instantiate_desc),
4041
ValidArgs: []string{"1"},
4142
RunE: func(cmd *cobra.Command, args []string) error {
4243
return chaincodeDeploy(cmd, args, cf)
@@ -60,23 +61,23 @@ func instantiate(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envel
6061

6162
creator, err := cf.Signer.Serialize()
6263
if err != nil {
63-
return nil, fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err)
64+
return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
6465
}
6566

6667
prop, _, err := utils.CreateDeployProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc))
6768
if err != nil {
68-
return nil, fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
69+
return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
6970
}
7071

7172
var signedProp *pb.SignedProposal
7273
signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
7374
if err != nil {
74-
return nil, fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err)
75+
return nil, fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err)
7576
}
7677

7778
proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
7879
if err != nil {
79-
return nil, fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err)
80+
return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
8081
}
8182

8283
if proposalResponse != nil {

peer/chaincode/invoke.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func invokeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
2929
chaincodeInvokeCmd = &cobra.Command{
3030
Use: "invoke",
3131
Short: fmt.Sprintf("Invoke the specified %s.", chainFuncName),
32-
Long: fmt.Sprintf(`Invoke the specified %s. It will try to commit the endorsed transaction to the network.`, chainFuncName),
32+
Long: fmt.Sprintf("Invoke the specified %s. It will try to commit the endorsed transaction to the network.", chainFuncName),
3333
ValidArgs: []string{"1"},
3434
RunE: func(cmd *cobra.Command, args []string) error {
3535
return chaincodeInvoke(cmd, args, cf)

peer/chaincode/package.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28+
const package_desc = "Package the specified chaincode into a deployment spec."
29+
2830
// deployCmd returns the cobra command for Chaincode Deploy
2931
func packageCmd(cf *ChaincodeCmdFactory) *cobra.Command {
3032
chaincodeInstantiateCmd = &cobra.Command{
3133
Use: "package",
32-
Short: fmt.Sprintf("Package the specified chaincode into a deployment spec."),
33-
Long: fmt.Sprintf(`Package the specified chaincode into a deployment spec.`),
34+
Short: package_desc,
35+
Long: package_desc,
3436
ValidArgs: []string{"1"},
3537
RunE: func(cmd *cobra.Command, args []string) error {
3638
return chaincodePackage(cmd, args, cf)

peer/chaincode/query.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func queryCmd(cf *ChaincodeCmdFactory) *cobra.Command {
2929
chaincodeQueryCmd = &cobra.Command{
3030
Use: "query",
3131
Short: fmt.Sprintf("Query using the specified %s.", chainFuncName),
32-
Long: fmt.Sprintf(`Get endorsed result of %s function call and print it. It won't generate transaction.`, chainFuncName),
32+
Long: fmt.Sprintf("Get endorsed result of %s function call and print it. It won't generate transaction.", chainFuncName),
3333
ValidArgs: []string{"1"},
3434
RunE: func(cmd *cobra.Command, args []string) error {
3535
return chaincodeQuery(cmd, args, cf)

peer/chaincode/upgrade.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ package chaincode
1919
import (
2020
"fmt"
2121

22-
"golang.org/x/net/context"
23-
2422
protcommon "github.com/hyperledger/fabric/protos/common"
2523
pb "github.com/hyperledger/fabric/protos/peer"
2624
"github.com/hyperledger/fabric/protos/utils"
2725
"github.com/spf13/cobra"
26+
"golang.org/x/net/context"
2827
)
2928

3029
var chaincodeUpgradeCmd *cobra.Command
@@ -35,8 +34,8 @@ const upgrade_cmdname = "upgrade"
3534
func upgradeCmd(cf *ChaincodeCmdFactory) *cobra.Command {
3635
chaincodeUpgradeCmd = &cobra.Command{
3736
Use: upgrade_cmdname,
38-
Short: fmt.Sprintf("Upgrade chaincode."),
39-
Long: fmt.Sprintf(`Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed.`),
37+
Short: "Upgrade chaincode.",
38+
Long: "Upgrade an existing chaincode with the specified one. The new chaincode will immediately replace the existing chaincode upon the transaction committed.",
4039
ValidArgs: []string{"1"},
4140
RunE: func(cmd *cobra.Command, args []string) error {
4241
return chaincodeUpgrade(cmd, args, cf)
@@ -60,24 +59,24 @@ func upgrade(cmd *cobra.Command, cf *ChaincodeCmdFactory) (*protcommon.Envelope,
6059

6160
creator, err := cf.Signer.Serialize()
6261
if err != nil {
63-
return nil, fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err)
62+
return nil, fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err)
6463
}
6564

6665
prop, _, err := utils.CreateUpgradeProposalFromCDS(chainID, cds, creator, policyMarhsalled, []byte(escc), []byte(vscc))
6766
if err != nil {
68-
return nil, fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
67+
return nil, fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err)
6968
}
7069
logger.Debugf("Get upgrade proposal for chaincode <%v>", spec.ChaincodeId)
7170

7271
var signedProp *pb.SignedProposal
7372
signedProp, err = utils.GetSignedProposal(prop, cf.Signer)
7473
if err != nil {
75-
return nil, fmt.Errorf("Error creating signed proposal %s: %s\n", chainFuncName, err)
74+
return nil, fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err)
7675
}
7776

7877
proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
7978
if err != nil {
80-
return nil, fmt.Errorf("Error endorsing %s: %s\n", chainFuncName, err)
79+
return nil, fmt.Errorf("Error endorsing %s: %s", chainFuncName, err)
8180
}
8281
logger.Debugf("endorse upgrade proposal, get response <%v>", proposalResponse.Response)
8382

peer/chaincode/upgrade_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
package chaincode
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"sync"
2324
"testing"
2425

26+
"github.com/hyperledger/fabric/msp/mgmt/testtools"
2527
"github.com/hyperledger/fabric/peer/common"
2628
pb "github.com/hyperledger/fabric/protos/peer"
27-
// "github.com/hyperledger/fabric/protos/utils"
28-
29-
"github.com/hyperledger/fabric/msp/mgmt/testtools"
3029
)
3130

3231
var once sync.Once
@@ -141,7 +140,7 @@ func TestUpgradeCmdSendTXFail(t *testing.T) {
141140

142141
mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil)
143142

144-
sendErr := fmt.Errorf("send tx failed")
143+
sendErr := errors.New("send tx failed")
145144
mockBroadcastClient := common.GetMockBroadcastClient(sendErr)
146145

147146
mockCF := &ChaincodeCmdFactory{

0 commit comments

Comments
 (0)