Skip to content

Commit

Permalink
Merge "[FAB-6073] Update shim errors to new errors pkg"
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rWin authored and Gerrit Code Review committed Sep 14, 2017
2 parents 6ccecef + 65a9128 commit 93e66e6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 90 deletions.
44 changes: 22 additions & 22 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package shim

import (
"errors"
"flag"
"fmt"
"io"
Expand All @@ -37,6 +36,7 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -100,7 +100,7 @@ func userChaincodeStreamGetter(name string) (PeerChaincodeStream, error) {
clientConn, err := newPeerClientConnection()
if err != nil {
chaincodeLogger.Errorf("Error trying to connect to local peer: %s", err)
return nil, fmt.Errorf("Error trying to connect to local peer: %s", err)
return nil, errors.Wrap(err, "error trying to connect to local peer")
}

chaincodeLogger.Debugf("os.Args returns: %s", os.Args)
Expand All @@ -110,7 +110,7 @@ func userChaincodeStreamGetter(name string) (PeerChaincodeStream, error) {
// Establish stream with validating peer
stream, err := chaincodeSupportClient.Register(context.Background())
if err != nil {
return nil, fmt.Errorf("Error chatting with leader at address=%s: %s", getPeerAddress(), err)
return nil, errors.WithMessage(err, fmt.Sprintf("error chatting with leader at address=%s", getPeerAddress()))
}

return stream, nil
Expand All @@ -124,12 +124,12 @@ func Start(cc Chaincode) error {

chaincodename := viper.GetString("chaincode.id.name")
if chaincodename == "" {
return fmt.Errorf("Error chaincode id not provided")
return errors.New("error chaincode id not provided")
}

err := factory.InitFactories(factory.GetDefaultOpts())
if err != nil {
return fmt.Errorf("Internal error, BCCSP could not be initialized with default options: %s", err)
return errors.WithMessage(err, "internal error, BCCSP could not be initialized with default options")
}

//mock stream not set up ... get real stream
Expand Down Expand Up @@ -216,7 +216,7 @@ func StartInProc(env []string, args []string, cc Chaincode, recv <-chan *pb.Chai
}
}
if chaincodename == "" {
return fmt.Errorf("Error chaincode id not provided")
return errors.New("error chaincode id not provided")
}

stream := newInProcStream(recv, send)
Expand Down Expand Up @@ -255,12 +255,12 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
chaincodeID := &pb.ChaincodeID{Name: chaincodename}
payload, err := proto.Marshal(chaincodeID)
if err != nil {
return fmt.Errorf("Error marshalling chaincodeID during chaincode registration: %s", err)
return errors.Wrap(err, "error marshalling chaincodeID during chaincode registration")
}
// Register on the stream
chaincodeLogger.Debugf("Registering.. sending %s", pb.ChaincodeMessage_REGISTER)
if err = handler.serialSend(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTER, Payload: payload}); err != nil {
return fmt.Errorf("Error sending chaincode REGISTER: %s", err)
return errors.WithMessage(err, "error sending chaincode REGISTER")
}
waitc := make(chan struct{})
errc := make(chan error)
Expand Down Expand Up @@ -289,7 +289,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
continue
}
//no, bail
err = fmt.Errorf("Error sending %s: %s", in.Type.String(), sendErr)
err = errors.Wrap(sendErr, fmt.Sprintf("error sending %s", in.Type.String()))
return
case in = <-msgAvail:
if err == io.EOF {
Expand All @@ -299,7 +299,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
chaincodeLogger.Errorf("Received error from server: %s, ending chaincode stream", err)
return
} else if in == nil {
err = fmt.Errorf("Received nil message, ending chaincode stream")
err = errors.New("received nil message, ending chaincode stream")
chaincodeLogger.Debug("Received nil message, ending chaincode stream")
return
}
Expand All @@ -316,7 +316,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
// Call FSM.handleMessage()
err = handler.handleMessage(in)
if err != nil {
err = fmt.Errorf("Error handling message: %s", err)
err = errors.WithMessage(err, "error handling message")
return
}

Expand Down Expand Up @@ -353,18 +353,18 @@ func (stub *ChaincodeStub) init(handler *Handler, txid string, input *pb.Chainco

stub.proposal, err = utils.GetProposal(signedProposal.ProposalBytes)
if err != nil {
return fmt.Errorf("Failed extracting signedProposal from signed signedProposal. [%s]", err)
return errors.WithMessage(err, "failed extracting signedProposal from signed signedProposal")
}

// Extract creator, transient, binding...
stub.creator, stub.transient, err = utils.GetChaincodeProposalContext(stub.proposal)
if err != nil {
return fmt.Errorf("Failed extracting signedProposal fields. [%s]", err)
return errors.WithMessage(err, "failed extracting signedProposal fields")
}

stub.binding, err = utils.ComputeProposalBinding(stub.proposal)
if err != nil {
return fmt.Errorf("Failed computing binding from signedProposal. [%s]", err)
return errors.WithMessage(err, "failed computing binding from signedProposal")
}
}

Expand Down Expand Up @@ -404,7 +404,7 @@ func (stub *ChaincodeStub) GetState(key string) ([]byte, error) {
// PutState documentation can be found in interfaces.go
func (stub *ChaincodeStub) PutState(key string, value []byte) error {
if key == "" {
return fmt.Errorf("key must not be an empty string")
return errors.New("key must not be an empty string")
}
return stub.handler.handlePutState(key, value, stub.TxID)
}
Expand Down Expand Up @@ -514,11 +514,11 @@ func splitCompositeKey(compositeKey string) (string, []string, error) {

func validateCompositeKeyAttribute(str string) error {
if !utf8.ValidString(str) {
return fmt.Errorf("Not a valid utf8 string: [%x]", str)
return errors.Errorf("not a valid utf8 string: [%x]", str)
}
for index, runeValue := range str {
if runeValue == minUnicodeRuneValue || runeValue == maxUnicodeRuneValue {
return fmt.Errorf(`Input contain unicode %#U starting at position [%d]. %#U and %#U are not allowed in the input attribute of a composite key`,
return errors.Errorf(`input contain unicode %#U starting at position [%d]. %#U and %#U are not allowed in the input attribute of a composite key`,
runeValue, index, minUnicodeRuneValue, maxUnicodeRuneValue)
}
}
Expand All @@ -532,7 +532,7 @@ func validateCompositeKeyAttribute(str string) error {
func validateSimpleKeys(simpleKeys ...string) error {
for _, key := range simpleKeys {
if len(key) > 0 && key[0] == compositeKeyNamespace[0] {
return fmt.Errorf(`First character of the key [%s] contains a null character which is not allowed`, key)
return errors.Errorf(`first character of the key [%s] contains a null character which is not allowed`, key)
}
}
return nil
Expand Down Expand Up @@ -597,7 +597,7 @@ func (iter *CommonIterator) getResultFromBytes(queryResultBytes *pb.QueryResultB
}
return historyQueryResult, nil
}
return nil, errors.New("Wrong result type")
return nil, errors.New("wrong result type")
}

func (iter *CommonIterator) fetchNextQueryResult() error {
Expand Down Expand Up @@ -634,12 +634,12 @@ func (iter *CommonIterator) nextResult(rType resultType) (commonledger.QueryResu
return queryResult, err
} else if !iter.response.HasMore {
// On call to Next() without check of HasMore
return nil, errors.New("No such key")
return nil, errors.New("no such key")
}

// should not fall through here
// case: no cached results but HasMore is true.
return nil, errors.New("Invalid iterator state")
return nil, errors.New("invalid iterator state")
}

// Close documentation can be found in interfaces.go
Expand Down Expand Up @@ -724,7 +724,7 @@ func (stub *ChaincodeStub) GetTxTimestamp() (*timestamp.Timestamp, error) {
// SetEvent documentation can be found in interfaces.go
func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error {
if name == "" {
return errors.New("Event name can not be nil string.")
return errors.New("event name can not be nil string")
}
stub.chaincodeEvent = &pb.ChaincodeEvent{EventName: name, Payload: payload}
return nil
Expand Down
13 changes: 7 additions & 6 deletions core/chaincode/shim/ext/encshim/encshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/chaincode/shim/ext/entities"
"github.com/pkg/errors"
)

type encShimImpl struct {
Expand All @@ -20,7 +21,7 @@ type encShimImpl struct {

func NewEncShim(stub shim.ChaincodeStubInterface) (EncShim, error) {
if stub == nil {
return nil, fmt.Errorf("NewEncShim error, nil stub")
return nil, errors.New("NewEncShim error, nil stub")
}

return &encShimImpl{
Expand All @@ -40,12 +41,12 @@ func (s *encShimImpl) GetState(key string) ([]byte, error) {
// stub is guaranteed to be valid by the constructor

if s.ent == nil {
return nil, fmt.Errorf("nil entity, With should have been called")
return nil, errors.New("nil entity, With should have been called")
}

ciphertext, err := s.stub.GetState(key)
if err != nil {
return nil, fmt.Errorf("GetState error, stub.GetState returned %s", err)
return nil, errors.WithMessage(err, "GetState error, stub.GetState returned")
} else if len(ciphertext) == 0 {
return nil, &NilKeyError{key: key}
}
Expand All @@ -57,17 +58,17 @@ func (s *encShimImpl) PutState(key string, value []byte) error {
// stub is guaranteed to be valid by the constructor

if s.ent == nil {
return fmt.Errorf("nil entity, With should have been called")
return errors.New("nil entity, With should have been called")
}

ciphertext, err := s.ent.Encrypt(value)
if err != nil {
return fmt.Errorf("PutState error, enc.Encrypt returned %s", err)
return errors.WithMessage(err, "PutState error, enc.Encrypt returned")
}

err = s.stub.PutState(key, ciphertext)
if err != nil {
return fmt.Errorf("PutState error, stub.PutState returned %s", err)
return errors.WithMessage(err, "PutState error, stub.PutState returned")
}

return nil
Expand Down
36 changes: 18 additions & 18 deletions core/chaincode/shim/ext/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ package entities

import (
"encoding/pem"
"fmt"
"reflect"

"github.com/hyperledger/fabric/bccsp"
"github.com/pkg/errors"
)

type pkiEntity struct {
Expand All @@ -26,12 +26,12 @@ type pkiEntity struct {
// capable of performing AES 256 bit encryption using PKCS#7 padding
func NewAES256EncrypterEntity(ID string, b bccsp.BCCSP, key []byte) (EncrypterEntity, error) {
if b == nil {
return nil, fmt.Errorf("nil BCCSP")
return nil, errors.New("nil BCCSP")
}

k, err := b.KeyImport(key, &bccsp.AES256ImportKeyOpts{Temporary: true})
if err != nil {
return nil, fmt.Errorf("bccspInst.KeyImport failed, err %s", err)
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}

return NewEncrypterEntity(ID, b, k, &bccsp.AESCBCPKCS7ModeOpts{}, &bccsp.AESCBCPKCS7ModeOpts{})
Expand All @@ -45,15 +45,15 @@ func NewAES256EncrypterEntity(ID string, b bccsp.BCCSP, key []byte) (EncrypterEn
// choose it in a way that it is meaningful
func NewEncrypterEntity(ID string, bccsp bccsp.BCCSP, eKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts) (EncrypterEntity, error) {
if ID == "" {
return nil, fmt.Errorf("NewEntity error: empty ID")
return nil, errors.New("NewEntity error: empty ID")
}

if bccsp == nil {
return nil, fmt.Errorf("NewEntity error: nil bccsp")
return nil, errors.New("NewEntity error: nil bccsp")
}

if eKey == nil {
return nil, fmt.Errorf("NewEntity error: nil keys")
return nil, errors.New("NewEntity error: nil keys")
}

return &pkiEntity{
Expand Down Expand Up @@ -115,7 +115,7 @@ func (pe *pkiEntity) Public() (Entity, error) {

if !pe.eKey.Symmetric() {
if eKeyPub, err = pe.eKey.PublicKey(); err != nil {
return nil, fmt.Errorf("Public error, eKey.PublicKey returned %s", err)
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
}
}

Expand All @@ -141,22 +141,22 @@ type pkiSigningEntity struct {
// signing using ECDSA
func NewAES256EncrypterECDSASignerEntity(ID string, b bccsp.BCCSP, encKeyBytes, signKeyBytes []byte) (EncrypterSignerEntity, error) {
if b == nil {
return nil, fmt.Errorf("nil BCCSP")
return nil, errors.New("nil BCCSP")
}

encKey, err := b.KeyImport(encKeyBytes, &bccsp.AES256ImportKeyOpts{Temporary: true})
if err != nil {
return nil, fmt.Errorf("bccspInst.KeyImport failed, err %s", err)
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}

bl, _ := pem.Decode(signKeyBytes)
if bl == nil {
return nil, fmt.Errorf("pem.Decode returns nil")
return nil, errors.New("pem.Decode returns nil")
}

signKey, err := b.KeyImport(bl.Bytes, &bccsp.ECDSAPrivateKeyImportOpts{Temporary: true})
if err != nil {
return nil, fmt.Errorf("bccspInst.KeyImport failed, err %s", err)
return nil, errors.WithMessage(err, "bccspInst.KeyImport failed")
}

return NewEncrypterSignerEntity(ID, b, encKey, signKey, &bccsp.AESCBCPKCS7ModeOpts{}, &bccsp.AESCBCPKCS7ModeOpts{}, nil, &bccsp.SHA256Opts{})
Expand All @@ -172,15 +172,15 @@ func NewAES256EncrypterECDSASignerEntity(ID string, b bccsp.BCCSP, encKeyBytes,
// to choose it in a way that it is meaningful
func NewEncrypterSignerEntity(ID string, bccsp bccsp.BCCSP, eKey, sKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts, sOpts bccsp.SignerOpts, hOpts bccsp.HashOpts) (EncrypterSignerEntity, error) {
if ID == "" {
return nil, fmt.Errorf("NewEntity error: empty ID")
return nil, errors.New("NewEntity error: empty ID")
}

if bccsp == nil {
return nil, fmt.Errorf("NewEntity error: nil bccsp")
return nil, errors.New("NewEntity error: nil bccsp")
}

if eKey == nil || sKey == nil {
return nil, fmt.Errorf("NewEntity error: nil keys")
return nil, errors.New("NewEntity error: nil keys")
}

return &pkiSigningEntity{
Expand All @@ -203,13 +203,13 @@ func (pe *pkiSigningEntity) Public() (Entity, error) {

if !pe.eKey.Symmetric() {
if eKeyPub, err = pe.eKey.PublicKey(); err != nil {
return nil, fmt.Errorf("Public error, eKey.PublicKey returned %s", err)
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
}
}

sKeyPub, err := pe.sKey.PublicKey()
if err != nil {
return nil, fmt.Errorf("Public error, sKey.PublicKey returned %s", err)
return nil, errors.WithMessage(err, "public error, sKey.PublicKey returned")
}

return &pkiSigningEntity{
Expand Down Expand Up @@ -237,7 +237,7 @@ func (this *pkiSigningEntity) Equals(e Entity) bool {
func (pe *pkiSigningEntity) Sign(msg []byte) ([]byte, error) {
h, err := pe.bccsp.Hash(msg, pe.hOpts)
if err != nil {
return nil, fmt.Errorf("Sign error: bccsp.Hash return %s", err)
return nil, errors.WithMessage(err, "sign error: bccsp.Hash returned")
}

return pe.bccsp.Sign(pe.sKey, h, pe.sOpts)
Expand All @@ -246,7 +246,7 @@ func (pe *pkiSigningEntity) Sign(msg []byte) ([]byte, error) {
func (pe *pkiSigningEntity) Verify(signature, msg []byte) (bool, error) {
h, err := pe.bccsp.Hash(msg, pe.hOpts)
if err != nil {
return false, fmt.Errorf("Sign error: bccsp.Hash return %s", err)
return false, errors.WithMessage(err, "sign error: bccsp.Hash returned")
}

return pe.bccsp.Verify(pe.sKey, signature, h, pe.sOpts)
Expand Down
Loading

0 comments on commit 93e66e6

Please sign in to comment.