Skip to content

Commit 65a9128

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

File tree

6 files changed

+92
-90
lines changed

6 files changed

+92
-90
lines changed

core/chaincode/shim/chaincode.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
package shim
2020

2121
import (
22-
"errors"
2322
"flag"
2423
"fmt"
2524
"io"
@@ -37,6 +36,7 @@ import (
3736
pb "github.com/hyperledger/fabric/protos/peer"
3837
"github.com/hyperledger/fabric/protos/utils"
3938
"github.com/op/go-logging"
39+
"github.com/pkg/errors"
4040
"github.com/spf13/viper"
4141
"golang.org/x/net/context"
4242
"google.golang.org/grpc"
@@ -100,7 +100,7 @@ func userChaincodeStreamGetter(name string) (PeerChaincodeStream, error) {
100100
clientConn, err := newPeerClientConnection()
101101
if err != nil {
102102
chaincodeLogger.Errorf("Error trying to connect to local peer: %s", err)
103-
return nil, fmt.Errorf("Error trying to connect to local peer: %s", err)
103+
return nil, errors.Wrap(err, "error trying to connect to local peer")
104104
}
105105

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

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

125125
chaincodename := viper.GetString("chaincode.id.name")
126126
if chaincodename == "" {
127-
return fmt.Errorf("Error chaincode id not provided")
127+
return errors.New("error chaincode id not provided")
128128
}
129129

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

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

222222
stream := newInProcStream(recv, send)
@@ -255,12 +255,12 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
255255
chaincodeID := &pb.ChaincodeID{Name: chaincodename}
256256
payload, err := proto.Marshal(chaincodeID)
257257
if err != nil {
258-
return fmt.Errorf("Error marshalling chaincodeID during chaincode registration: %s", err)
258+
return errors.Wrap(err, "error marshalling chaincodeID during chaincode registration")
259259
}
260260
// Register on the stream
261261
chaincodeLogger.Debugf("Registering.. sending %s", pb.ChaincodeMessage_REGISTER)
262262
if err = handler.serialSend(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTER, Payload: payload}); err != nil {
263-
return fmt.Errorf("Error sending chaincode REGISTER: %s", err)
263+
return errors.WithMessage(err, "error sending chaincode REGISTER")
264264
}
265265
waitc := make(chan struct{})
266266
errc := make(chan error)
@@ -289,7 +289,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
289289
continue
290290
}
291291
//no, bail
292-
err = fmt.Errorf("Error sending %s: %s", in.Type.String(), sendErr)
292+
err = errors.Wrap(sendErr, fmt.Sprintf("error sending %s", in.Type.String()))
293293
return
294294
case in = <-msgAvail:
295295
if err == io.EOF {
@@ -299,7 +299,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
299299
chaincodeLogger.Errorf("Received error from server: %s, ending chaincode stream", err)
300300
return
301301
} else if in == nil {
302-
err = fmt.Errorf("Received nil message, ending chaincode stream")
302+
err = errors.New("received nil message, ending chaincode stream")
303303
chaincodeLogger.Debug("Received nil message, ending chaincode stream")
304304
return
305305
}
@@ -316,7 +316,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
316316
// Call FSM.handleMessage()
317317
err = handler.handleMessage(in)
318318
if err != nil {
319-
err = fmt.Errorf("Error handling message: %s", err)
319+
err = errors.WithMessage(err, "error handling message")
320320
return
321321
}
322322

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

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

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

365365
stub.binding, err = utils.ComputeProposalBinding(stub.proposal)
366366
if err != nil {
367-
return fmt.Errorf("Failed computing binding from signedProposal. [%s]", err)
367+
return errors.WithMessage(err, "failed computing binding from signedProposal")
368368
}
369369
}
370370

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

515515
func validateCompositeKeyAttribute(str string) error {
516516
if !utf8.ValidString(str) {
517-
return fmt.Errorf("Not a valid utf8 string: [%x]", str)
517+
return errors.Errorf("not a valid utf8 string: [%x]", str)
518518
}
519519
for index, runeValue := range str {
520520
if runeValue == minUnicodeRuneValue || runeValue == maxUnicodeRuneValue {
521-
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`,
521+
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`,
522522
runeValue, index, minUnicodeRuneValue, maxUnicodeRuneValue)
523523
}
524524
}
@@ -532,7 +532,7 @@ func validateCompositeKeyAttribute(str string) error {
532532
func validateSimpleKeys(simpleKeys ...string) error {
533533
for _, key := range simpleKeys {
534534
if len(key) > 0 && key[0] == compositeKeyNamespace[0] {
535-
return fmt.Errorf(`First character of the key [%s] contains a null character which is not allowed`, key)
535+
return errors.Errorf(`first character of the key [%s] contains a null character which is not allowed`, key)
536536
}
537537
}
538538
return nil
@@ -597,7 +597,7 @@ func (iter *CommonIterator) getResultFromBytes(queryResultBytes *pb.QueryResultB
597597
}
598598
return historyQueryResult, nil
599599
}
600-
return nil, errors.New("Wrong result type")
600+
return nil, errors.New("wrong result type")
601601
}
602602

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

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

645645
// Close documentation can be found in interfaces.go
@@ -724,7 +724,7 @@ func (stub *ChaincodeStub) GetTxTimestamp() (*timestamp.Timestamp, error) {
724724
// SetEvent documentation can be found in interfaces.go
725725
func (stub *ChaincodeStub) SetEvent(name string, payload []byte) error {
726726
if name == "" {
727-
return errors.New("Event name can not be nil string.")
727+
return errors.New("event name can not be nil string")
728728
}
729729
stub.chaincodeEvent = &pb.ChaincodeEvent{EventName: name, Payload: payload}
730730
return nil

core/chaincode/shim/ext/encshim/encshim.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/hyperledger/fabric/core/chaincode/shim"
1313
"github.com/hyperledger/fabric/core/chaincode/shim/ext/entities"
14+
"github.com/pkg/errors"
1415
)
1516

1617
type encShimImpl struct {
@@ -20,7 +21,7 @@ type encShimImpl struct {
2021

2122
func NewEncShim(stub shim.ChaincodeStubInterface) (EncShim, error) {
2223
if stub == nil {
23-
return nil, fmt.Errorf("NewEncShim error, nil stub")
24+
return nil, errors.New("NewEncShim error, nil stub")
2425
}
2526

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

4243
if s.ent == nil {
43-
return nil, fmt.Errorf("nil entity, With should have been called")
44+
return nil, errors.New("nil entity, With should have been called")
4445
}
4546

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

5960
if s.ent == nil {
60-
return fmt.Errorf("nil entity, With should have been called")
61+
return errors.New("nil entity, With should have been called")
6162
}
6263

6364
ciphertext, err := s.ent.Encrypt(value)
6465
if err != nil {
65-
return fmt.Errorf("PutState error, enc.Encrypt returned %s", err)
66+
return errors.WithMessage(err, "PutState error, enc.Encrypt returned")
6667
}
6768

6869
err = s.stub.PutState(key, ciphertext)
6970
if err != nil {
70-
return fmt.Errorf("PutState error, stub.PutState returned %s", err)
71+
return errors.WithMessage(err, "PutState error, stub.PutState returned")
7172
}
7273

7374
return nil

core/chaincode/shim/ext/entities/entities.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package entities
88

99
import (
1010
"encoding/pem"
11-
"fmt"
1211
"reflect"
1312

1413
"github.com/hyperledger/fabric/bccsp"
14+
"github.com/pkg/errors"
1515
)
1616

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

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

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

5151
if bccsp == nil {
52-
return nil, fmt.Errorf("NewEntity error: nil bccsp")
52+
return nil, errors.New("NewEntity error: nil bccsp")
5353
}
5454

5555
if eKey == nil {
56-
return nil, fmt.Errorf("NewEntity error: nil keys")
56+
return nil, errors.New("NewEntity error: nil keys")
5757
}
5858

5959
return &pkiEntity{
@@ -115,7 +115,7 @@ func (pe *pkiEntity) Public() (Entity, error) {
115115

116116
if !pe.eKey.Symmetric() {
117117
if eKeyPub, err = pe.eKey.PublicKey(); err != nil {
118-
return nil, fmt.Errorf("Public error, eKey.PublicKey returned %s", err)
118+
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
119119
}
120120
}
121121

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

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

152152
bl, _ := pem.Decode(signKeyBytes)
153153
if bl == nil {
154-
return nil, fmt.Errorf("pem.Decode returns nil")
154+
return nil, errors.New("pem.Decode returns nil")
155155
}
156156

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

162162
return NewEncrypterSignerEntity(ID, b, encKey, signKey, &bccsp.AESCBCPKCS7ModeOpts{}, &bccsp.AESCBCPKCS7ModeOpts{}, nil, &bccsp.SHA256Opts{})
@@ -172,15 +172,15 @@ func NewAES256EncrypterECDSASignerEntity(ID string, b bccsp.BCCSP, encKeyBytes,
172172
// to choose it in a way that it is meaningful
173173
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) {
174174
if ID == "" {
175-
return nil, fmt.Errorf("NewEntity error: empty ID")
175+
return nil, errors.New("NewEntity error: empty ID")
176176
}
177177

178178
if bccsp == nil {
179-
return nil, fmt.Errorf("NewEntity error: nil bccsp")
179+
return nil, errors.New("NewEntity error: nil bccsp")
180180
}
181181

182182
if eKey == nil || sKey == nil {
183-
return nil, fmt.Errorf("NewEntity error: nil keys")
183+
return nil, errors.New("NewEntity error: nil keys")
184184
}
185185

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

204204
if !pe.eKey.Symmetric() {
205205
if eKeyPub, err = pe.eKey.PublicKey(); err != nil {
206-
return nil, fmt.Errorf("Public error, eKey.PublicKey returned %s", err)
206+
return nil, errors.WithMessage(err, "public error, eKey.PublicKey returned")
207207
}
208208
}
209209

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

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

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

252252
return pe.bccsp.Verify(pe.sKey, signature, h, pe.sOpts)

0 commit comments

Comments
 (0)