Skip to content

Commit

Permalink
[FAB-2987] Update core/chaincode package errors
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
wlahti committed Sep 11, 2017
1 parent 0a25d07 commit 3dff5e9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 deletions.
5 changes: 2 additions & 3 deletions core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ package chaincode
import (
"context"

"fmt"

"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/ledger"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)

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

if data == nil || data.Vscc == "" || data.Policy == nil {
return "", nil, fmt.Errorf("Incorrect validation info in LSCC")
return "", nil, errors.New("incorrect validation info in LSCC")
}

return data.Vscc, data.Policy, nil
Expand Down
57 changes: 29 additions & 28 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/hyperledger/fabric/core/ledger"
pb "github.com/hyperledger/fabric/protos/peer"
logging "github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -244,7 +245,7 @@ type DuplicateChaincodeHandlerError struct {
}

func (d *DuplicateChaincodeHandlerError) Error() string {
return fmt.Sprintf("Duplicate chaincodeID error: %s", d.ChaincodeID)
return fmt.Sprintf("duplicate chaincodeID error: %s", d.ChaincodeID)
}

func newDuplicateChaincodeHandlerError(chaincodeHandler *Handler) error {
Expand Down Expand Up @@ -272,7 +273,7 @@ func (chaincodeSupport *ChaincodeSupport) registerHandler(chaincodehandler *Hand
if chaincodeSupport.userRunsCC == false {
//this chaincode was not launched by the peer and is attempting
//to register. Don't allow this.
return fmt.Errorf("peer will not accepting external chaincode connection %v (except in dev mode)", chaincodehandler.ChaincodeID)
return errors.Errorf("peer will not accept external chaincode connection %v (except in dev mode)", chaincodehandler.ChaincodeID)
}
chaincodeSupport.runningChaincodes.chaincodeMap[key] = &chaincodeRTEnv{handler: chaincodehandler}
}
Expand Down Expand Up @@ -303,7 +304,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
defer chaincodeSupport.runningChaincodes.Unlock()
if _, ok := chaincodeSupport.chaincodeHasBeenLaunched(key); !ok {
// Handler NOT found
return fmt.Errorf("Error deregistering handler, could not find handler with key: %s", key)
return errors.Errorf("error deregistering handler, could not find handler with key: %s", key)
}
delete(chaincodeSupport.runningChaincodes.chaincodeMap, key)
chaincodeLogger.Debugf("Deregistered handler with key: %s", key)
Expand All @@ -320,32 +321,32 @@ func (chaincodeSupport *ChaincodeSupport) sendReady(context context.Context, ccc
if chrte, ok = chaincodeSupport.chaincodeHasBeenLaunched(canName); !ok {
chaincodeSupport.runningChaincodes.Unlock()
chaincodeLogger.Debugf("handler not found for chaincode %s", canName)
return fmt.Errorf("handler not found for chaincode %s", canName)
return errors.Errorf("handler not found for chaincode %s", canName)
}
chaincodeSupport.runningChaincodes.Unlock()

var notfy chan *pb.ChaincodeMessage
var err error
if notfy, err = chrte.handler.ready(context, cccid.ChainID, cccid.TxID, cccid.SignedProposal, cccid.Proposal); err != nil {
return fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_READY, err)
return errors.WithMessage(err, fmt.Sprintf("error sending %s", pb.ChaincodeMessage_READY))
}
if notfy != nil {
select {
case ccMsg := <-notfy:
if ccMsg.Type == pb.ChaincodeMessage_ERROR {
err = fmt.Errorf("Error initializing container %s: %s", canName, string(ccMsg.Payload))
err = errors.Errorf("error initializing container %s: %s", canName, string(ccMsg.Payload))
}
if ccMsg.Type == pb.ChaincodeMessage_COMPLETED {
res := &pb.Response{}
_ = proto.Unmarshal(ccMsg.Payload, res)
if res.Status != shim.OK {
err = fmt.Errorf("Error initializing container %s: %s", canName, string(res.Message))
err = errors.Errorf("error initializing container %s: %s", canName, string(res.Message))
}
// TODO
// return res so that endorser can anylyze it.
}
case <-time.After(timeout):
err = fmt.Errorf("Timeout expired while executing send init message")
err = errors.New("timeout expired while executing send init message")
}
}

Expand Down Expand Up @@ -381,7 +382,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
if chaincodeSupport.peerTLS {
certKeyPair, err = chaincodeSupport.auth.Generate(cccid.GetCanonicalName())
if err != nil {
return nil, nil, fmt.Errorf("failed generating TLS cert for %s: %v", cccid.GetCanonicalName(), err)
return nil, nil, errors.WithMessage(err, fmt.Sprintf("failed generating TLS cert for %s", cccid.GetCanonicalName()))
}
envs = append(envs, "CORE_PEER_TLS_ENABLED=true")
if chaincodeSupport.peerTLSSvrHostOrd != "" {
Expand Down Expand Up @@ -411,7 +412,7 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
case pb.ChaincodeSpec_NODE:
args = []string{"/bin/sh", "-c", fmt.Sprintf("cd /usr/local/src; node chaincode.js --peer.address %s", chaincodeSupport.peerAddress)}
default:
return nil, nil, fmt.Errorf("Unknown chaincodeType: %s", cLang)
return nil, nil, errors.Errorf("unknown chaincodeType: %s", cLang)
}
chaincodeLogger.Debugf("Executable is %s", args[0])
chaincodeLogger.Debugf("Args %v", args)
Expand All @@ -423,15 +424,15 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec, cLang pb.ChaincodeSpec_Type, builder api.BuildSpecFactory) error {
canName := cccid.GetCanonicalName()
if canName == "" {
return fmt.Errorf("chaincode name not set")
return errors.New("chaincode name not set")
}

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

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

//Chaincode is not up and is not in the process of being launched. Setup flag
Expand Down Expand Up @@ -495,7 +496,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
if err == nil {
err = resp.(container.VMCResp).Err
}
err = fmt.Errorf("Error starting container: %s", err)
err = errors.WithMessage(err, "error starting container")
chaincodeSupport.runningChaincodes.Lock()
delete(chaincodeSupport.runningChaincodes.chaincodeMap, canName)
chaincodeSupport.runningChaincodes.Unlock()
Expand All @@ -506,10 +507,10 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
select {
case ok := <-notfy:
if !ok {
err = fmt.Errorf("registration failed for %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
err = errors.Errorf("registration failed for %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
}
case <-time.After(chaincodeSupport.ccStartupTimeout):
err = fmt.Errorf("Timeout expired while starting chaincode %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
err = errors.Errorf("timeout expired while starting chaincode %s(networkid:%s,peerid:%s,tx:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID, cccid.TxID)
}
if err != nil {
chaincodeLogger.Debugf("stopping due to error while launching %s", err)
Expand All @@ -525,7 +526,7 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cccid *ccprovider.CCContext, cds *pb.ChaincodeDeploymentSpec) error {
canName := cccid.GetCanonicalName()
if canName == "" {
return fmt.Errorf("chaincode name not set")
return errors.New("chaincode name not set")
}

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

_, err := container.VMCProcess(context, vmtype, sir)
if err != nil {
err = fmt.Errorf("Error stopping container: %s", err)
err = errors.WithMessage(err, "error stopping container")
//but proceed to cleanup
}

Expand Down Expand Up @@ -587,7 +588,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
if !chrte.handler.registered {
chaincodeSupport.runningChaincodes.Unlock()
chaincodeLogger.Debugf("premature execution - chaincode (%s) launched and waiting for registration", canName)
err = fmt.Errorf("premature execution - chaincode (%s) launched and waiting for registration", canName)
err = errors.Errorf("premature execution - chaincode (%s) launched and waiting for registration", canName)
return cID, cMsg, err
}
if chrte.handler.isRunning() {
Expand All @@ -605,15 +606,15 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
//herds
if chaincodeSupport.launchStarted(canName) {
chaincodeSupport.runningChaincodes.Unlock()
err = fmt.Errorf("premature execution - chaincode (%s) is being launched", canName)
err = errors.Errorf("premature execution - chaincode (%s) is being launched", canName)
return cID, cMsg, err
}
}
chaincodeSupport.runningChaincodes.Unlock()

if cds == nil {
if cccid.Syscc {
return cID, cMsg, fmt.Errorf("a syscc should be running (it cannot be launched) %s", canName)
return cID, cMsg, errors.Errorf("a syscc should be running (it cannot be launched) %s", canName)
}

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

cds = &pb.ChaincodeDeploymentSpec{}

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

Expand Down Expand Up @@ -682,7 +683,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, cccid
err = chaincodeSupport.sendReady(context, cccid, chaincodeSupport.ccStartupTimeout)
if err != nil {
chaincodeLogger.Errorf("sending init failed(%s)", err)
err = fmt.Errorf("Failed to init chaincode(%s)", err)
err = errors.WithMessage(err, "failed to init chaincode")
errIgnore := chaincodeSupport.Stop(context, cccid, cds)
if errIgnore != nil {
chaincodeLogger.Errorf("stop failed %s(%s)", errIgnore, err)
Expand Down Expand Up @@ -737,22 +738,22 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, cccid *c
if !ok {
chaincodeSupport.runningChaincodes.Unlock()
chaincodeLogger.Debugf("cannot execute-chaincode is not running: %s", canName)
return nil, fmt.Errorf("Cannot execute transaction for %s", canName)
return nil, errors.Errorf("cannot execute transaction for %s", canName)
}
chaincodeSupport.runningChaincodes.Unlock()

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

//our responsibility to delete transaction context if sendExecuteMessage succeeded
Expand Down
9 changes: 5 additions & 4 deletions core/chaincode/chaincodeexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)

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

return res.Payload, nil
Expand All @@ -60,7 +61,7 @@ func GetChaincodeDataFromLSCC(ctxt context.Context, txid string, signedProp *pb.
res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
if err == nil {
if res.Status != shim.OK {
return nil, fmt.Errorf("%s", res.Message)
return nil, errors.New(res.Message)
}
cd := &ccprovider.ChaincodeData{}
err = proto.Unmarshal(res.Payload, cd)
Expand All @@ -84,7 +85,7 @@ func ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args []
res, ccevent, err = Execute(ctxt, cccid, spec)
if err != nil {
chaincodeLogger.Errorf("Error executing chaincode: %s", err)
return nil, nil, fmt.Errorf("Error executing chaincode: %s", err)
return nil, nil, errors.WithMessage(err, "error executing chaincode")
}

return res, ccevent, err
Expand Down
17 changes: 9 additions & 8 deletions core/chaincode/exectransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
"golang.org/x/net/context"
)

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

_, cMsg, err := theChaincodeSupport.Launch(ctxt, cccid, spec)
if err != nil {
return nil, nil, fmt.Errorf("%s", err)
return nil, nil, err
}

cMsg.Decorations = cccid.ProposalDecorations

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

resp, err := theChaincodeSupport.Execute(ctxt, cccid, ccMsg, theChaincodeSupport.executetimeout)
if err != nil {
// Rollback transaction
return nil, nil, fmt.Errorf("Failed to execute transaction (%s)", err)
return nil, nil, errors.WithMessage(err, "failed to execute transaction")
} else if resp == nil {
// Rollback transaction
return nil, nil, fmt.Errorf("Failed to receive a response for (%s)", cccid.TxID)
return nil, nil, errors.Errorf("failed to receive a response for txid (%s)", cccid.TxID)
}

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

// Success
return res, resp.ChaincodeEvent, nil
} else if resp.Type == pb.ChaincodeMessage_ERROR {
// Rollback transaction
return nil, resp.ChaincodeEvent, fmt.Errorf("Transaction returned with failure: %s", string(resp.Payload))
return nil, resp.ChaincodeEvent, errors.Errorf("transaction returned with failure: %s", string(resp.Payload))
}

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

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

if res.Status != shim.OK {
return nil, nil, fmt.Errorf("%s", res.Message)
return nil, nil, errors.New(res.Message)
}

return res.Payload, event, nil
Expand Down
Loading

0 comments on commit 3dff5e9

Please sign in to comment.