@@ -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
246247func (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
250251func 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
423424func (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.
525526func (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
0 commit comments