@@ -38,6 +38,7 @@ import (
38
38
"github.com/hyperledger/fabric/core/ledger"
39
39
pb "github.com/hyperledger/fabric/protos/peer"
40
40
logging "github.com/op/go-logging"
41
+ "github.com/pkg/errors"
41
42
"github.com/spf13/viper"
42
43
"golang.org/x/net/context"
43
44
)
@@ -244,7 +245,7 @@ type DuplicateChaincodeHandlerError struct {
244
245
}
245
246
246
247
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 )
248
249
}
249
250
250
251
func newDuplicateChaincodeHandlerError (chaincodeHandler * Handler ) error {
@@ -272,7 +273,7 @@ func (chaincodeSupport *ChaincodeSupport) registerHandler(chaincodehandler *Hand
272
273
if chaincodeSupport .userRunsCC == false {
273
274
//this chaincode was not launched by the peer and is attempting
274
275
//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 )
276
277
}
277
278
chaincodeSupport .runningChaincodes .chaincodeMap [key ] = & chaincodeRTEnv {handler : chaincodehandler }
278
279
}
@@ -303,7 +304,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
303
304
defer chaincodeSupport .runningChaincodes .Unlock ()
304
305
if _ , ok := chaincodeSupport .chaincodeHasBeenLaunched (key ); ! ok {
305
306
// 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 )
307
308
}
308
309
delete (chaincodeSupport .runningChaincodes .chaincodeMap , key )
309
310
chaincodeLogger .Debugf ("Deregistered handler with key: %s" , key )
@@ -320,32 +321,32 @@ func (chaincodeSupport *ChaincodeSupport) sendReady(context context.Context, ccc
320
321
if chrte , ok = chaincodeSupport .chaincodeHasBeenLaunched (canName ); ! ok {
321
322
chaincodeSupport .runningChaincodes .Unlock ()
322
323
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 )
324
325
}
325
326
chaincodeSupport .runningChaincodes .Unlock ()
326
327
327
328
var notfy chan * pb.ChaincodeMessage
328
329
var err error
329
330
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 ) )
331
332
}
332
333
if notfy != nil {
333
334
select {
334
335
case ccMsg := <- notfy :
335
336
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 ))
337
338
}
338
339
if ccMsg .Type == pb .ChaincodeMessage_COMPLETED {
339
340
res := & pb.Response {}
340
341
_ = proto .Unmarshal (ccMsg .Payload , res )
341
342
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 ))
343
344
}
344
345
// TODO
345
346
// return res so that endorser can anylyze it.
346
347
}
347
348
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" )
349
350
}
350
351
}
351
352
@@ -381,7 +382,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
381
382
if chaincodeSupport .peerTLS {
382
383
certKeyPair , err = chaincodeSupport .auth .Generate (cccid .GetCanonicalName ())
383
384
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 ()) )
385
386
}
386
387
envs = append (envs , "CORE_PEER_TLS_ENABLED=true" )
387
388
if chaincodeSupport .peerTLSSvrHostOrd != "" {
@@ -411,7 +412,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
411
412
case pb .ChaincodeSpec_NODE :
412
413
args = []string {"/bin/sh" , "-c" , fmt .Sprintf ("cd /usr/local/src; node chaincode.js --peer.address %s" , chaincodeSupport .peerAddress )}
413
414
default :
414
- return nil , nil , fmt .Errorf ("Unknown chaincodeType: %s" , cLang )
415
+ return nil , nil , errors .Errorf ("unknown chaincodeType: %s" , cLang )
415
416
}
416
417
chaincodeLogger .Debugf ("Executable is %s" , args [0 ])
417
418
chaincodeLogger .Debugf ("Args %v" , args )
@@ -423,15 +424,15 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
423
424
func (chaincodeSupport * ChaincodeSupport ) launchAndWaitForRegister (ctxt context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec , cLang pb.ChaincodeSpec_Type , builder api.BuildSpecFactory ) error {
424
425
canName := cccid .GetCanonicalName ()
425
426
if canName == "" {
426
- return fmt . Errorf ("chaincode name not set" )
427
+ return errors . New ("chaincode name not set" )
427
428
}
428
429
429
430
chaincodeSupport .runningChaincodes .Lock ()
430
431
//if its in the map, its either up or being launched. Either case break the
431
432
//multiple launch by failing
432
433
if _ , hasBeenLaunched := chaincodeSupport .chaincodeHasBeenLaunched (canName ); hasBeenLaunched {
433
434
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 )
435
436
}
436
437
437
438
//prohibit multiple simultaneous invokes (for example while flooding the
@@ -442,7 +443,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
442
443
//until the container is up and registered.
443
444
if chaincodeSupport .launchStarted (canName ) {
444
445
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 )
446
447
}
447
448
448
449
//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.
495
496
if err == nil {
496
497
err = resp .(container.VMCResp ).Err
497
498
}
498
- err = fmt . Errorf ( "Error starting container: %s" , err )
499
+ err = errors . WithMessage ( err , "error starting container" )
499
500
chaincodeSupport .runningChaincodes .Lock ()
500
501
delete (chaincodeSupport .runningChaincodes .chaincodeMap , canName )
501
502
chaincodeSupport .runningChaincodes .Unlock ()
@@ -506,10 +507,10 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
506
507
select {
507
508
case ok := <- notfy :
508
509
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 )
510
511
}
511
512
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 )
513
514
}
514
515
if err != nil {
515
516
chaincodeLogger .Debugf ("stopping due to error while launching %s" , err )
@@ -525,7 +526,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
525
526
func (chaincodeSupport * ChaincodeSupport ) Stop (context context.Context , cccid * ccprovider.CCContext , cds * pb.ChaincodeDeploymentSpec ) error {
526
527
canName := cccid .GetCanonicalName ()
527
528
if canName == "" {
528
- return fmt . Errorf ("chaincode name not set" )
529
+ return errors . New ("chaincode name not set" )
529
530
}
530
531
531
532
//stop the chaincode
@@ -538,7 +539,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *c
538
539
539
540
_ , err := container .VMCProcess (context , vmtype , sir )
540
541
if err != nil {
541
- err = fmt . Errorf ( "Error stopping container: %s" , err )
542
+ err = errors . WithMessage ( err , "error stopping container" )
542
543
//but proceed to cleanup
543
544
}
544
545
@@ -587,7 +588,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
587
588
if ! chrte .handler .registered {
588
589
chaincodeSupport .runningChaincodes .Unlock ()
589
590
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 )
591
592
return cID , cMsg , err
592
593
}
593
594
if chrte .handler .isRunning () {
@@ -605,15 +606,15 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
605
606
//herds
606
607
if chaincodeSupport .launchStarted (canName ) {
607
608
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 )
609
610
return cID , cMsg , err
610
611
}
611
612
}
612
613
chaincodeSupport .runningChaincodes .Unlock ()
613
614
614
615
if cds == nil {
615
616
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 )
617
618
}
618
619
619
620
if chaincodeSupport .userRunsCC {
@@ -626,18 +627,18 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
626
627
//this will also validate the ID from the LSCC
627
628
depPayload , err = GetCDSFromLSCC (context , cccid .TxID , cccid .SignedProposal , cccid .Proposal , cccid .ChainID , cID .Name )
628
629
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 ) )
630
631
}
631
632
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 ) )
633
634
}
634
635
635
636
cds = & pb.ChaincodeDeploymentSpec {}
636
637
637
638
//Get lang from original deployment
638
639
err = proto .Unmarshal (depPayload , cds )
639
640
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 ) )
641
642
}
642
643
}
643
644
@@ -682,7 +683,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
682
683
err = chaincodeSupport .sendReady (context , cccid , chaincodeSupport .ccStartupTimeout )
683
684
if err != nil {
684
685
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" )
686
687
errIgnore := chaincodeSupport .Stop (context , cccid , cds )
687
688
if errIgnore != nil {
688
689
chaincodeLogger .Errorf ("stop failed %s(%s)" , errIgnore , err )
@@ -737,22 +738,22 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *c
737
738
if ! ok {
738
739
chaincodeSupport .runningChaincodes .Unlock ()
739
740
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 )
741
742
}
742
743
chaincodeSupport .runningChaincodes .Unlock ()
743
744
744
745
var notfy chan * pb.ChaincodeMessage
745
746
var err error
746
747
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" ) )
748
749
}
749
750
var ccresp * pb.ChaincodeMessage
750
751
select {
751
752
case ccresp = <- notfy :
752
753
//response is sent to user or calling chaincode. ChaincodeMessage_ERROR
753
754
//are typically treated as error
754
755
case <- time .After (timeout ):
755
- err = fmt . Errorf ( "Timeout expired while executing transaction" )
756
+ err = errors . New ( "timeout expired while executing transaction" )
756
757
}
757
758
758
759
//our responsibility to delete transaction context if sendExecuteMessage succeeded
0 commit comments