Skip to content

Commit 3dff5e9

Browse files
committed
[FAB-2987] Update core/chaincode package errors
This CR updates the errors for core/chaincode from fmt.Errorf() to the newly vendored errors package. Change-Id: Ifaad34b25befabc96c55e0a3b85d7f23fe4bfe3b Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent 0a25d07 commit 3dff5e9

File tree

5 files changed

+73
-70
lines changed

5 files changed

+73
-70
lines changed

core/chaincode/ccproviderimpl.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package chaincode
1919
import (
2020
"context"
2121

22-
"fmt"
23-
2422
"github.com/hyperledger/fabric/core/common/ccprovider"
2523
"github.com/hyperledger/fabric/core/ledger"
2624
pb "github.com/hyperledger/fabric/protos/peer"
25+
"github.com/pkg/errors"
2726
)
2827

2928
// ccProviderFactory implements the ccprovider.ChaincodeProviderFactory
@@ -87,7 +86,7 @@ func (c *ccProviderImpl) GetCCValidationInfoFromLSCC(ctxt context.Context, txid
8786
}
8887

8988
if data == nil || data.Vscc == "" || data.Policy == nil {
90-
return "", nil, fmt.Errorf("Incorrect validation info in LSCC")
89+
return "", nil, errors.New("incorrect validation info in LSCC")
9190
}
9291

9392
return data.Vscc, data.Policy, nil

core/chaincode/chaincode_support.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/hyperledger/fabric/core/ledger"
3939
pb "github.com/hyperledger/fabric/protos/peer"
4040
logging "github.com/op/go-logging"
41+
"github.com/pkg/errors"
4142
"github.com/spf13/viper"
4243
"golang.org/x/net/context"
4344
)
@@ -244,7 +245,7 @@ type DuplicateChaincodeHandlerError struct {
244245
}
245246

246247
func (d *DuplicateChaincodeHandlerError) Error() string {
247-
return fmt.Sprintf("Duplicate chaincodeID error: %s", d.ChaincodeID)
248+
return fmt.Sprintf("duplicate chaincodeID error: %s", d.ChaincodeID)
248249
}
249250

250251
func newDuplicateChaincodeHandlerError(chaincodeHandler *Handler) error {
@@ -272,7 +273,7 @@ func (chaincodeSupport *ChaincodeSupport) registerHandler(chaincodehandler *Hand
272273
if chaincodeSupport.userRunsCC == false {
273274
//this chaincode was not launched by the peer and is attempting
274275
//to register. Don't allow this.
275-
return fmt.Errorf("peer will not accepting external chaincode connection %v (except in dev mode)", chaincodehandler.ChaincodeID)
276+
return errors.Errorf("peer will not accept external chaincode connection %v (except in dev mode)", chaincodehandler.ChaincodeID)
276277
}
277278
chaincodeSupport.runningChaincodes.chaincodeMap[key] = &chaincodeRTEnv{handler: chaincodehandler}
278279
}
@@ -303,7 +304,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
303304
defer chaincodeSupport.runningChaincodes.Unlock()
304305
if _, ok := chaincodeSupport.chaincodeHasBeenLaunched(key); !ok {
305306
// Handler NOT found
306-
return fmt.Errorf("Error deregistering handler, could not find handler with key: %s", key)
307+
return errors.Errorf("error deregistering handler, could not find handler with key: %s", key)
307308
}
308309
delete(chaincodeSupport.runningChaincodes.chaincodeMap, key)
309310
chaincodeLogger.Debugf("Deregistered handler with key: %s", key)
@@ -320,32 +321,32 @@ func (chaincodeSupport *ChaincodeSupport) sendReady(context context.Context, ccc
320321
if chrte, ok = chaincodeSupport.chaincodeHasBeenLaunched(canName); !ok {
321322
chaincodeSupport.runningChaincodes.Unlock()
322323
chaincodeLogger.Debugf("handler not found for chaincode %s", canName)
323-
return fmt.Errorf("handler not found for chaincode %s", canName)
324+
return errors.Errorf("handler not found for chaincode %s", canName)
324325
}
325326
chaincodeSupport.runningChaincodes.Unlock()
326327

327328
var notfy chan *pb.ChaincodeMessage
328329
var err error
329330
if notfy, err = chrte.handler.ready(context, cccid.ChainID, cccid.TxID, cccid.SignedProposal, cccid.Proposal); err != nil {
330-
return fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_READY, err)
331+
return errors.WithMessage(err, fmt.Sprintf("error sending %s", pb.ChaincodeMessage_READY))
331332
}
332333
if notfy != nil {
333334
select {
334335
case ccMsg := <-notfy:
335336
if ccMsg.Type == pb.ChaincodeMessage_ERROR {
336-
err = fmt.Errorf("Error initializing container %s: %s", canName, string(ccMsg.Payload))
337+
err = errors.Errorf("error initializing container %s: %s", canName, string(ccMsg.Payload))
337338
}
338339
if ccMsg.Type == pb.ChaincodeMessage_COMPLETED {
339340
res := &pb.Response{}
340341
_ = proto.Unmarshal(ccMsg.Payload, res)
341342
if res.Status != shim.OK {
342-
err = fmt.Errorf("Error initializing container %s: %s", canName, string(res.Message))
343+
err = errors.Errorf("error initializing container %s: %s", canName, string(res.Message))
343344
}
344345
// TODO
345346
// return res so that endorser can anylyze it.
346347
}
347348
case <-time.After(timeout):
348-
err = fmt.Errorf("Timeout expired while executing send init message")
349+
err = errors.New("timeout expired while executing send init message")
349350
}
350351
}
351352

@@ -381,7 +382,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
381382
if chaincodeSupport.peerTLS {
382383
certKeyPair, err = chaincodeSupport.auth.Generate(cccid.GetCanonicalName())
383384
if err != nil {
384-
return nil, nil, fmt.Errorf("failed generating TLS cert for %s: %v", cccid.GetCanonicalName(), err)
385+
return nil, nil, errors.WithMessage(err, fmt.Sprintf("failed generating TLS cert for %s", cccid.GetCanonicalName()))
385386
}
386387
envs = append(envs, "CORE_PEER_TLS_ENABLED=true")
387388
if chaincodeSupport.peerTLSSvrHostOrd != "" {
@@ -411,7 +412,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
411412
case pb.ChaincodeSpec_NODE:
412413
args = []string{"/bin/sh", "-c", fmt.Sprintf("cd /usr/local/src; node chaincode.js --peer.address %s", chaincodeSupport.peerAddress)}
413414
default:
414-
return nil, nil, fmt.Errorf("Unknown chaincodeType: %s", cLang)
415+
return nil, nil, errors.Errorf("unknown chaincodeType: %s", cLang)
415416
}
416417
chaincodeLogger.Debugf("Executable is %s", args[0])
417418
chaincodeLogger.Debugf("Args %v", args)
@@ -423,15 +424,15 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
423424
func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec, cLang pb.ChaincodeSpec_Type, builder api.BuildSpecFactory) error {
424425
canName := cccid.GetCanonicalName()
425426
if canName == "" {
426-
return fmt.Errorf("chaincode name not set")
427+
return errors.New("chaincode name not set")
427428
}
428429

429430
chaincodeSupport.runningChaincodes.Lock()
430431
//if its in the map, its either up or being launched. Either case break the
431432
//multiple launch by failing
432433
if _, hasBeenLaunched := chaincodeSupport.chaincodeHasBeenLaunched(canName); hasBeenLaunched {
433434
chaincodeSupport.runningChaincodes.Unlock()
434-
return fmt.Errorf("Error chaincode has been launched: %s", canName)
435+
return errors.Errorf("error chaincode has been launched: %s", canName)
435436
}
436437

437438
//prohibit multiple simultaneous invokes (for example while flooding the
@@ -442,7 +443,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
442443
//until the container is up and registered.
443444
if chaincodeSupport.launchStarted(canName) {
444445
chaincodeSupport.runningChaincodes.Unlock()
445-
return fmt.Errorf("Error chaincode is already launching: %s", canName)
446+
return errors.Errorf("error chaincode is already launching: %s", canName)
446447
}
447448

448449
//Chaincode is not up and is not in the process of being launched. Setup flag
@@ -495,7 +496,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
495496
if err == nil {
496497
err = resp.(container.VMCResp).Err
497498
}
498-
err = fmt.Errorf("Error starting container: %s", err)
499+
err = errors.WithMessage(err, "error starting container")
499500
chaincodeSupport.runningChaincodes.Lock()
500501
delete(chaincodeSupport.runningChaincodes.chaincodeMap, canName)
501502
chaincodeSupport.runningChaincodes.Unlock()
@@ -506,10 +507,10 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
506507
select {
507508
case ok := <-notfy:
508509
if !ok {
509-
err = fmt.Errorf("registration failed for %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
510+
err = errors.Errorf("registration failed for %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
510511
}
511512
case <-time.After(chaincodeSupport.ccStartupTimeout):
512-
err = fmt.Errorf("Timeout expired while starting chaincode %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
513+
err = errors.Errorf("timeout expired while starting chaincode %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
513514
}
514515
if err != nil {
515516
chaincodeLogger.Debugf("stopping due to error while launching %s", err)
@@ -525,7 +526,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
525526
func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
526527
canName := cccid.GetCanonicalName()
527528
if canName == "" {
528-
return fmt.Errorf("chaincode name not set")
529+
return errors.New("chaincode name not set")
529530
}
530531

531532
//stop the chaincode
@@ -538,7 +539,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *c
538539

539540
_, err := container.VMCProcess(context, vmtype, sir)
540541
if err != nil {
541-
err = fmt.Errorf("Error stopping container: %s", err)
542+
err = errors.WithMessage(err, "error stopping container")
542543
//but proceed to cleanup
543544
}
544545

@@ -587,7 +588,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
587588
if !chrte.handler.registered {
588589
chaincodeSupport.runningChaincodes.Unlock()
589590
chaincodeLogger.Debugf("premature execution - chaincode (%s) launched and waiting for registration", canName)
590-
err = fmt.Errorf("premature execution - chaincode (%s) launched and waiting for registration", canName)
591+
err = errors.Errorf("premature execution - chaincode (%s) launched and waiting for registration", canName)
591592
return cID, cMsg, err
592593
}
593594
if chrte.handler.isRunning() {
@@ -605,15 +606,15 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
605606
//herds
606607
if chaincodeSupport.launchStarted(canName) {
607608
chaincodeSupport.runningChaincodes.Unlock()
608-
err = fmt.Errorf("premature execution - chaincode (%s) is being launched", canName)
609+
err = errors.Errorf("premature execution - chaincode (%s) is being launched", canName)
609610
return cID, cMsg, err
610611
}
611612
}
612613
chaincodeSupport.runningChaincodes.Unlock()
613614

614615
if cds == nil {
615616
if cccid.Syscc {
616-
return cID, cMsg, fmt.Errorf("a syscc should be running (it cannot be launched) %s", canName)
617+
return cID, cMsg, errors.Errorf("a syscc should be running (it cannot be launched) %s", canName)
617618
}
618619

619620
if chaincodeSupport.userRunsCC {
@@ -626,18 +627,18 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
626627
//this will also validate the ID from the LSCC
627628
depPayload, err = GetCDSFromLSCC(context, cccid.TxID, cccid.SignedProposal, cccid.Proposal, cccid.ChainID, cID.Name)
628629
if err != nil {
629-
return cID, cMsg, fmt.Errorf("Could not get deployment transaction from LSCC for %s - %s", canName, err)
630+
return cID, cMsg, errors.WithMessage(err, fmt.Sprintf("could not get deployment transaction from LSCC for %s", canName))
630631
}
631632
if depPayload == nil {
632-
return cID, cMsg, fmt.Errorf("failed to get deployment payload %s - %s", canName, err)
633+
return cID, cMsg, errors.WithMessage(err, fmt.Sprintf("failed to get deployment payload %s", canName))
633634
}
634635

635636
cds = &pb.ChaincodeDeploymentSpec{}
636637

637638
//Get lang from original deployment
638639
err = proto.Unmarshal(depPayload, cds)
639640
if err != nil {
640-
return cID, cMsg, fmt.Errorf("failed to unmarshal deployment transactions for %s - %s", canName, err)
641+
return cID, cMsg, errors.Wrap(err, fmt.Sprintf("failed to unmarshal deployment transactions for %s", canName))
641642
}
642643
}
643644

@@ -682,7 +683,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
682683
err = chaincodeSupport.sendReady(context, cccid, chaincodeSupport.ccStartupTimeout)
683684
if err != nil {
684685
chaincodeLogger.Errorf("sending init failed(%s)", err)
685-
err = fmt.Errorf("Failed to init chaincode(%s)", err)
686+
err = errors.WithMessage(err, "failed to init chaincode")
686687
errIgnore := chaincodeSupport.Stop(context, cccid, cds)
687688
if errIgnore != nil {
688689
chaincodeLogger.Errorf("stop failed %s(%s)", errIgnore, err)
@@ -737,22 +738,22 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *c
737738
if !ok {
738739
chaincodeSupport.runningChaincodes.Unlock()
739740
chaincodeLogger.Debugf("cannot execute-chaincode is not running: %s", canName)
740-
return nil, fmt.Errorf("Cannot execute transaction for %s", canName)
741+
return nil, errors.Errorf("cannot execute transaction for %s", canName)
741742
}
742743
chaincodeSupport.runningChaincodes.Unlock()
743744

744745
var notfy chan *pb.ChaincodeMessage
745746
var err error
746747
if notfy, err = chrte.handler.sendExecuteMessage(ctxt, cccid.ChainID, msg, cccid.SignedProposal, cccid.Proposal); err != nil {
747-
return nil, fmt.Errorf("Error sending %s: %s", msg.Type.String(), err)
748+
return nil, errors.WithMessage(err, fmt.Sprintf("error sending"))
748749
}
749750
var ccresp *pb.ChaincodeMessage
750751
select {
751752
case ccresp = <-notfy:
752753
//response is sent to user or calling chaincode. ChaincodeMessage_ERROR
753754
//are typically treated as error
754755
case <-time.After(timeout):
755-
err = fmt.Errorf("Timeout expired while executing transaction")
756+
err = errors.New("timeout expired while executing transaction")
756757
}
757758

758759
//our responsibility to delete transaction context if sendExecuteMessage succeeded

core/chaincode/chaincodeexec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/hyperledger/fabric/core/chaincode/shim"
2727
"github.com/hyperledger/fabric/core/common/ccprovider"
2828
pb "github.com/hyperledger/fabric/protos/peer"
29+
"github.com/pkg/errors"
2930
)
3031

3132
//create a chaincode invocation spec
@@ -44,10 +45,10 @@ func GetCDSFromLSCC(ctxt context.Context, txid string, signedProp *pb.SignedProp
4445
cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
4546
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
4647
if err != nil {
47-
return nil, fmt.Errorf("Execute getdepspec(%s, %s) of LSCC error: %s", chainID, chaincodeID, err)
48+
return nil, errors.WithMessage(err, fmt.Sprintf("execute getdepspec(%s, %s) of LSCC error", chainID, chaincodeID))
4849
}
4950
if res.Status != shim.OK {
50-
return nil, fmt.Errorf("Get ChaincodeDeploymentSpec for %s/%s from LSCC error: %s", chaincodeID, chainID, res.Message)
51+
return nil, errors.Errorf("get ChaincodeDeploymentSpec for %s/%s from LSCC error: %s", chaincodeID, chainID, res.Message)
5152
}
5253

5354
return res.Payload, nil
@@ -60,7 +61,7 @@ func GetChaincodeDataFromLSCC(ctxt context.Context, txid string, signedProp *pb.
6061
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
6162
if err == nil {
6263
if res.Status != shim.OK {
63-
return nil, fmt.Errorf("%s", res.Message)
64+
return nil, errors.New(res.Message)
6465
}
6566
cd := &ccprovider.ChaincodeData{}
6667
err = proto.Unmarshal(res.Payload, cd)
@@ -84,7 +85,7 @@ func ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args []
8485
res, ccevent, err = Execute(ctxt, cccid, spec)
8586
if err != nil {
8687
chaincodeLogger.Errorf("Error executing chaincode: %s", err)
87-
return nil, nil, fmt.Errorf("Error executing chaincode: %s", err)
88+
return nil, nil, errors.WithMessage(err, "error executing chaincode")
8889
}
8990

9091
return res, ccevent, err

core/chaincode/exectransaction.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/hyperledger/fabric/core/chaincode/shim"
2424
"github.com/hyperledger/fabric/core/common/ccprovider"
2525
pb "github.com/hyperledger/fabric/protos/peer"
26+
"github.com/pkg/errors"
2627
"golang.org/x/net/context"
2728
)
2829

@@ -43,24 +44,24 @@ func Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}
4344

4445
_, cMsg, err := theChaincodeSupport.Launch(ctxt, cccid, spec)
4546
if err != nil {
46-
return nil, nil, fmt.Errorf("%s", err)
47+
return nil, nil, err
4748
}
4849

4950
cMsg.Decorations = cccid.ProposalDecorations
5051

5152
var ccMsg *pb.ChaincodeMessage
5253
ccMsg, err = createCCMessage(cctyp, cccid.TxID, cMsg)
5354
if err != nil {
54-
return nil, nil, fmt.Errorf("Failed to transaction message(%s)", err)
55+
return nil, nil, errors.WithMessage(err, "failed to create chaincode message")
5556
}
5657

5758
resp, err := theChaincodeSupport.Execute(ctxt, cccid, ccMsg, theChaincodeSupport.executetimeout)
5859
if err != nil {
5960
// Rollback transaction
60-
return nil, nil, fmt.Errorf("Failed to execute transaction (%s)", err)
61+
return nil, nil, errors.WithMessage(err, "failed to execute transaction")
6162
} else if resp == nil {
6263
// Rollback transaction
63-
return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", cccid.TxID)
64+
return nil, nil, errors.Errorf("failed to receive a response for txid (%s)", cccid.TxID)
6465
}
6566

6667
if resp.ChaincodeEvent != nil {
@@ -72,18 +73,18 @@ func Execute(ctxt context.Context, cccid *ccprovider.CCContext, spec interface{}
7273
res := &pb.Response{}
7374
unmarshalErr := proto.Unmarshal(resp.Payload, res)
7475
if unmarshalErr != nil {
75-
return nil, nil, fmt.Errorf("Failed to unmarshal response for (%s): %s", cccid.TxID, unmarshalErr)
76+
return nil, nil, errors.Wrap(unmarshalErr, fmt.Sprintf("failed to unmarshal response for txid (%s)", cccid.TxID))
7677
}
7778

7879
// Success
7980
return res, resp.ChaincodeEvent, nil
8081
} else if resp.Type == pb.ChaincodeMessage_ERROR {
8182
// Rollback transaction
82-
return nil, resp.ChaincodeEvent, fmt.Errorf("Transaction returned with failure: %s", string(resp.Payload))
83+
return nil, resp.ChaincodeEvent, errors.Errorf("transaction returned with failure: %s", string(resp.Payload))
8384
}
8485

8586
//TODO - this should never happen ... a panic is more appropriate but will save that for future
86-
return nil, nil, fmt.Errorf("receive a response for (%s) but in invalid state(%d)", cccid.TxID, resp.Type)
87+
return nil, nil, errors.Errorf("receive a response for txid (%s) but in invalid state (%d)", cccid.TxID, resp.Type)
8788
}
8889

8990
// ExecuteWithErrorFilter is similar to Execute, but filters error contained in chaincode response and returns Payload of response only.
@@ -101,7 +102,7 @@ func ExecuteWithErrorFilter(ctxt context.Context, cccid *ccprovider.CCContext, s
101102
}
102103

103104
if res.Status != shim.OK {
104-
return nil, nil, fmt.Errorf("%s", res.Message)
105+
return nil, nil, errors.New(res.Message)
105106
}
106107

107108
return res.Payload, event, nil

0 commit comments

Comments
 (0)