Skip to content

Commit e66f621

Browse files
refactor(fabricx): consistent use of errors package
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent c643f48 commit e66f621

File tree

22 files changed

+115
-118
lines changed

22 files changed

+115
-118
lines changed

platform/fabricx/core/committer/txhandler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package committer
88

99
import (
1010
"context"
11-
"fmt"
1211

1312
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1413
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
@@ -37,7 +36,7 @@ type handler struct {
3736

3837
func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.BlockMetadata, tx committer.CommitTx) (*committer.FinalityEvent, error) {
3938
if len(blkMetadata.Metadata) < statusIdx {
40-
return nil, fmt.Errorf("block metadata lacks transaction filter")
39+
return nil, errors.New("block metadata lacks transaction filter")
4140
}
4241

4342
statusCode := protoblocktx.Status(blkMetadata.Metadata[statusIdx][tx.TxNum])
@@ -61,7 +60,7 @@ func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.
6160
// escaping the switch and discard
6261
break
6362
}
64-
return nil, fmt.Errorf("failed committing transaction [txID=%s]: %w", event.TxID, err)
63+
return nil, errors.Wrapf(err, "committing endorser transaction [txID=%s]", event.TxID)
6564
}
6665
if !processed {
6766
logger.Debugf("TODO: Should we try to get chaincode events?")
@@ -74,7 +73,7 @@ func (h *handler) HandleFabricxTransaction(ctx context.Context, blkMetadata *cb.
7473

7574
logger.Warnf("discarding transaction [txID=%s] [reason=%v]", tx.TxID, statusCode.String())
7675
if err := h.committer.DiscardEndorserTransaction(ctx, event.TxID, tx.BlkNum, tx.Raw, event); err != nil {
77-
return nil, fmt.Errorf("failed discarding transaction [txID=%s]: %w", event.TxID, err)
76+
return nil, errors.Wrapf(err, "discarding endorser transaction [txID=%s]", event.TxID)
7877
}
7978

8079
return event, nil

platform/fabricx/core/finality/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ SPDX-License-Identifier: Apache-2.0
77
package finality
88

99
import (
10-
"fmt"
1110
"time"
11+
12+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1213
)
1314

1415
const DefaultRequestTimeout = 30 * time.Second
@@ -37,7 +38,7 @@ func NewConfig(configService ConfigService) (*Config, error) {
3738

3839
err := configService.UnmarshalKey("notificationService", &config)
3940
if err != nil {
40-
return config, fmt.Errorf("cannot get notify service config: %w", err)
41+
return config, errors.Wrap(err, "unmarshal notificationService")
4142
}
4243

4344
return config, nil

platform/fabricx/core/finality/grpc.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ SPDX-License-Identifier: Apache-2.0
77
package finality
88

99
import (
10-
"errors"
11-
"fmt"
1210
"os"
1311
"time"
1412

13+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1514
"google.golang.org/grpc"
1615
"google.golang.org/grpc/backoff"
1716
"google.golang.org/grpc/credentials"
1817
"google.golang.org/grpc/credentials/insecure"
1918
)
2019

21-
var ErrInvalidAddress = fmt.Errorf("empty address")
20+
var ErrInvalidAddress = errors.New("empty address")
2221

2322
func GrpcClient(c *Config) (*grpc.ClientConn, error) {
2423
// no endpoints in config
2524
if len(c.Endpoints) != 1 {
26-
return nil, fmt.Errorf("we need a single endpoint")
25+
return nil, errors.New("we need a single endpoint")
2726
}
2827

2928
// currently we only support connections to a single query service

platform/fabricx/core/finality/nlm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ package finality
88

99
import (
1010
"context"
11-
"errors"
1211
"slices"
1312
"sync"
1413
"time"
1514

15+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1616
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
1717
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
1818
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"

platform/fabricx/core/finality/nlm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ package finality
88

99
import (
1010
"context"
11-
"errors"
1211
"sync"
1312
"sync/atomic"
1413
"testing"
1514
"time"
1615

16+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1717
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
1818
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
1919
fdriver "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"

platform/fabricx/core/finality/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ package finality
88

99
import (
1010
"context"
11-
"errors"
1211
"reflect"
1312
"sync"
1413
"testing"
1514
"time"
1615

16+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1717
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
1818
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/driver/config"
1919
mock2 "github.com/hyperledger-labs/fabric-smart-client/platform/fabricx/core/finality/mock"

platform/fabricx/core/ledger/dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ package ledger
88

99
import (
1010
"context"
11-
"errors"
1211
"sync"
1312

13+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1414
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
1515
cb "github.com/hyperledger/fabric-protos-go-apiv2/common"
1616
)

platform/fabricx/core/ledger/ledger.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package ledger
88

99
import (
1010
"context"
11-
"fmt"
1211
"sync"
1312
"time"
1413

@@ -53,7 +52,7 @@ func (c *ledger) OnBlock(_ context.Context, block *cb.Block) (bool, error) {
5352
for i, tx := range block.Data.Data {
5453
_, _, chdr, err := fabricutils.UnmarshalTx(tx)
5554
if err != nil {
56-
return false, err
55+
return false, errors.Wrapf(err, "unmarshal transaction channel header")
5756
}
5857

5958
statusCode := protoblocktx.Status(block.Metadata.Metadata[cb.BlockMetadataIndex_TRANSACTIONS_FILTER][i])
@@ -89,10 +88,10 @@ func (c *ledger) GetTransactionByID(txID string) (driver.ProcessedTransaction, e
8988
status, ok := c.statuses[txID]
9089
c.mu.RUnlock()
9190
if ok {
92-
logger.Debugf("Transaction [%s] found with status [%d]", txID, int32(status))
91+
logger.Debugf("Transaction [txID=%s] found with status [%d]", txID, int32(status))
9392
return &liteTx{txID: txID, validationCode: status}, nil
9493
}
95-
logger.Warnf("transaction [%s] not found. retrying...", txID)
94+
logger.Warnf("Transaction [txID=%s] not found. retrying...", txID)
9695
time.Sleep(1 * time.Second)
9796
}
9897

@@ -107,13 +106,13 @@ func (c *ledger) GetBlockNumberByTxID(txID string) (uint64, error) {
107106
blockNum, ok := c.blockNums[txID]
108107
c.mu.RUnlock()
109108
if ok {
110-
logger.Debugf("Transaction [%s] found with blockNum [%v]", txID, blockNum)
109+
logger.Debugf("Transaction [txID=%s] found with blockNum [%v]", txID, blockNum)
111110
return blockNum, nil
112111
}
113-
logger.Warnf("transaction [%s] not found. retrying...")
112+
logger.Warnf("Transaction [txID=%s] not found. retrying...", txID)
114113
time.Sleep(1 * time.Second)
115114
}
116-
return 0, fmt.Errorf("transaction [%s] not found", txID)
115+
return 0, errors.Errorf("transaction [txID=%s] not found", txID)
117116
}
118117

119118
func (c *ledger) GetBlockByNumber(number uint64) (driver.Block, error) {

platform/fabricx/core/membership/endpoint.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ SPDX-License-Identifier: Apache-2.0
77
package membership
88

99
import (
10-
"fmt"
1110
"regexp"
1211
"strconv"
12+
13+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1314
)
1415

1516
const (
@@ -19,7 +20,7 @@ const (
1920

2021
var (
2122
myExp = regexp.MustCompile(`id=(\d+),(` + OrdererBroadcastType + `|` + OrdererDeliverType + `),(.*)`)
22-
ErrInvalidEndpointFormat = fmt.Errorf("invalid endpoint format")
23+
ErrInvalidEndpointFormat = errors.New("invalid endpoint format")
2324
)
2425

2526
type endpoint struct {
@@ -37,7 +38,7 @@ func parseEndpoint(str string) (*endpoint, error) {
3738

3839
id, err := strconv.Atoi(match[1])
3940
if err != nil {
40-
return nil, fmt.Errorf("invalid endpoint id: %w", err)
41+
return nil, errors.Wrap(err, "invalid endpoint id")
4142
}
4243

4344
return &endpoint{

platform/fabricx/core/membership/membership.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package membership
88

99
import (
10-
"fmt"
1110
"sync"
1211

1312
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -72,30 +71,30 @@ func (c *Service) DryUpdate(env *cb.Envelope) error {
7271
func (c *Service) validateConfig(env *cb.Envelope) (*channelconfig.Bundle, error) {
7372
payload, err := protoutil.UnmarshalPayload(env.Payload)
7473
if err != nil {
75-
return nil, errors.Wrapf(err, "cannot get payload from config transaction")
74+
return nil, errors.Wrapf(err, "unmarshal common payload")
7675
}
7776

7877
cenv, err := configtx.UnmarshalConfigEnvelope(payload.Data)
7978
if err != nil {
80-
return nil, errors.Wrapf(err, "error unmarshalling config which passed initial validity checks")
79+
return nil, errors.Wrapf(err, "unmarshal config envelope")
8180
}
8281

8382
// check if config tx is valid
8483
if c.channelResources != nil {
8584
v := c.channelResources.ConfigtxValidator()
8685
if err := v.Validate(cenv); err != nil {
87-
return nil, errors.Wrap(err, "failed to validate config transaction")
86+
return nil, errors.Wrap(err, "validate config transaction")
8887
}
8988
}
9089

9190
bundle, err := channelconfig.NewBundle(c.channelID, cenv.Config, factory.GetDefault())
9291
if err != nil {
93-
return nil, errors.Wrapf(err, "failed to build a new bundle")
92+
return nil, errors.Wrapf(err, "build a new bundle")
9493
}
9594

9695
channelconfig.LogSanityChecks(bundle)
9796
if err := capabilitiesSupported(bundle); err != nil {
98-
return nil, err
97+
return nil, errors.Wrapf(err, "check bundle capabilities")
9998
}
10099

101100
return bundle, nil
@@ -108,11 +107,11 @@ func capabilitiesSupported(res channelconfig.Resources) error {
108107
}
109108

110109
if err := ac.Capabilities().Supported(); err != nil {
111-
return errors.Wrapf(err, "[Channel %s] incompatible", res.ConfigtxValidator().ChannelID())
110+
return errors.Wrapf(err, "[Channel %s] application config capabilities incompatible", res.ConfigtxValidator().ChannelID())
112111
}
113112

114113
if err := res.ChannelConfig().Capabilities().Supported(); err != nil {
115-
return errors.Wrapf(err, "[Channel %s] incompatible", res.ConfigtxValidator().ChannelID())
114+
return errors.Wrapf(err, "[Channel %s] channel config capabilities incompatible", res.ConfigtxValidator().ChannelID())
116115
}
117116

118117
return nil
@@ -121,7 +120,7 @@ func capabilitiesSupported(res channelconfig.Resources) error {
121120
func (c *Service) IsValid(identity view.Identity) error {
122121
id, err := c.resources().MSPManager().DeserializeIdentity(identity)
123122
if err != nil {
124-
return errors.Wrapf(err, "failed deserializing identity [%s]", identity.String())
123+
return errors.Wrapf(err, "deserializing identity [%s]", identity.String())
125124
}
126125

127126
return id.Validate()
@@ -130,7 +129,7 @@ func (c *Service) IsValid(identity view.Identity) error {
130129
func (c *Service) GetVerifier(identity view.Identity) (driver.Verifier, error) {
131130
id, err := c.resources().MSPManager().DeserializeIdentity(identity)
132131
if err != nil {
133-
return nil, errors.Wrapf(err, "failed deserializing identity [%s]", identity.String())
132+
return nil, errors.Wrapf(err, "deserializing identity [%s]", identity.String())
134133
}
135134
return id, nil
136135
}
@@ -154,7 +153,7 @@ func (c *Service) GetMSPIDs() []string {
154153
func (c *Service) OrdererConfig(cs driver.ConfigService) (string, []*grpc.ConnectionConfig, error) {
155154
oc, ok := c.resources().OrdererConfig()
156155
if !ok || oc.Organizations() == nil {
157-
return "", nil, fmt.Errorf("orderer config does not exist")
156+
return "", nil, errors.New("orderer config does not exist")
158157
}
159158

160159
tlsEnabled, isSet := cs.OrderingTLSEnabled()
@@ -182,7 +181,7 @@ func (c *Service) OrdererConfig(cs driver.ConfigService) (string, []*grpc.Connec
182181

183182
ep, err := parseEndpoint(epStr)
184183
if err != nil {
185-
return "", nil, fmt.Errorf("cannot parse orderer endpoint [%s]: %w", epStr, err)
184+
return "", nil, errors.Wrapf(err, "parse orderer endpoint [%s]", epStr)
186185
}
187186
logger.Debugf("new OS endpoint: %s", epStr)
188187

0 commit comments

Comments
 (0)