Skip to content

Commit 239abb1

Browse files
authored
refactor: use errors.New to replace fmt.Errorf with no parameters (#5165)
Signed-off-by: LesCyber <[email protected]>
1 parent 2fe94d8 commit 239abb1

File tree

10 files changed

+41
-35
lines changed

10 files changed

+41
-35
lines changed

cmd/osnadmin/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"crypto/tls"
1212
"crypto/x509"
1313
"encoding/json"
14+
"errors"
1415
"fmt"
1516
"io"
1617
"net/http"
@@ -80,7 +81,7 @@ func executeForArgs(args []string) (output string, exit int, err error) {
8081
return "", 1, fmt.Errorf("reading orderer CA certificate: %s", err)
8182
}
8283
if !caCertPool.AppendCertsFromPEM(caFilePEM) {
83-
return "", 1, fmt.Errorf("failed to add ca-file PEM to cert pool")
84+
return "", 1, errors.New("failed to add ca-file PEM to cert pool")
8485
}
8586

8687
tlsClientCert, err = tls.LoadX509KeyPair(*clientCert, *clientKey)

core/common/ccprovider/cc_statedb_artifacts_provider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package ccprovider
99
import (
1010
"archive/tar"
1111
"bytes"
12-
"fmt"
12+
"errors"
1313
"io"
1414
"path/filepath"
1515
"strings"
@@ -47,7 +47,7 @@ func ExtractStatedbArtifactsFromCCPackage(ccpackage CCPackage) (statedbArtifacts
4747
metaprov, err := MetadataAsTarEntries(cds.CodePackage)
4848
if err != nil {
4949
ccproviderLogger.Infof("invalid deployment spec: %s", err)
50-
return nil, fmt.Errorf("invalid deployment spec")
50+
return nil, errors.New("invalid deployment spec")
5151
}
5252
return metaprov, nil
5353
}

core/common/ccprovider/cdspackage.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package ccprovider
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"hash"
2223
"os"
@@ -141,11 +142,11 @@ func (ccpack *CDSPackage) getCDSData(cds *pb.ChaincodeDeploymentSpec) ([]byte, [
141142
// ChaincodeDeploymentSpec
142143
func (ccpack *CDSPackage) ValidateCC(ccdata *ChaincodeData) error {
143144
if ccpack.depSpec == nil {
144-
return fmt.Errorf("uninitialized package")
145+
return errors.New("uninitialized package")
145146
}
146147

147148
if ccpack.data == nil {
148-
return fmt.Errorf("nil data")
149+
return errors.New("nil data")
149150
}
150151

151152
// This is a hack. LSCC expects a specific LSCC error when names are invalid so it
@@ -169,7 +170,7 @@ func (ccpack *CDSPackage) ValidateCC(ccdata *ChaincodeData) error {
169170
}
170171

171172
if !proto.Equal(ccpack.data, otherdata) {
172-
return fmt.Errorf("data mismatch")
173+
return errors.New("data mismatch")
173174
}
174175

175176
return nil
@@ -180,7 +181,7 @@ func (ccpack *CDSPackage) InitFromBuffer(buf []byte) (*ChaincodeData, error) {
180181
depSpec := &pb.ChaincodeDeploymentSpec{}
181182
err := proto.Unmarshal(buf, depSpec)
182183
if err != nil {
183-
return nil, fmt.Errorf("failed to unmarshal deployment spec from bytes")
184+
return nil, errors.New("failed to unmarshal deployment spec from bytes")
184185
}
185186

186187
databytes, id, data, err := ccpack.getCDSData(depSpec)
@@ -224,23 +225,23 @@ func (ccpack *CDSPackage) InitFromFS(ccNameVersion string) ([]byte, *pb.Chaincod
224225
// PutChaincodeToFS - serializes chaincode to a package on the file system
225226
func (ccpack *CDSPackage) PutChaincodeToFS() error {
226227
if ccpack.buf == nil {
227-
return fmt.Errorf("uninitialized package")
228+
return errors.New("uninitialized package")
228229
}
229230

230231
if ccpack.id == nil {
231-
return fmt.Errorf("id cannot be nil if buf is not nil")
232+
return errors.New("id cannot be nil if buf is not nil")
232233
}
233234

234235
if ccpack.depSpec == nil {
235-
return fmt.Errorf("depspec cannot be nil if buf is not nil")
236+
return errors.New("depspec cannot be nil if buf is not nil")
236237
}
237238

238239
if ccpack.data == nil {
239-
return fmt.Errorf("nil data")
240+
return errors.New("nil data")
240241
}
241242

242243
if ccpack.datab == nil {
243-
return fmt.Errorf("nil data bytes")
244+
return errors.New("nil data bytes")
244245
}
245246

246247
ccname := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Name

core/common/ccprovider/sigcdspackage.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package ccprovider
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223

@@ -180,15 +181,15 @@ func (ccpack *SignedCDSPackage) getCDSData(scds *pb.SignedChaincodeDeploymentSpe
180181
// ChaincodeDeploymentSpec
181182
func (ccpack *SignedCDSPackage) ValidateCC(ccdata *ChaincodeData) error {
182183
if ccpack.sDepSpec == nil {
183-
return fmt.Errorf("uninitialized package")
184+
return errors.New("uninitialized package")
184185
}
185186

186187
if ccpack.sDepSpec.ChaincodeDeploymentSpec == nil {
187-
return fmt.Errorf("signed chaincode deployment spec cannot be nil in a package")
188+
return errors.New("signed chaincode deployment spec cannot be nil in a package")
188189
}
189190

190191
if ccpack.depSpec == nil {
191-
return fmt.Errorf("chaincode deployment spec cannot be nil in a package")
192+
return errors.New("chaincode deployment spec cannot be nil in a package")
192193
}
193194

194195
// This is a hack. LSCC expects a specific LSCC error when names are invalid so it
@@ -212,7 +213,7 @@ func (ccpack *SignedCDSPackage) ValidateCC(ccdata *ChaincodeData) error {
212213
}
213214

214215
if !proto.Equal(ccpack.data, otherdata) {
215-
return fmt.Errorf("data mismatch")
216+
return errors.New("data mismatch")
216217
}
217218

218219
return nil
@@ -223,21 +224,21 @@ func (ccpack *SignedCDSPackage) InitFromBuffer(buf []byte) (*ChaincodeData, erro
223224
env := &common.Envelope{}
224225
err := proto.Unmarshal(buf, env)
225226
if err != nil {
226-
return nil, fmt.Errorf("failed to unmarshal envelope from bytes")
227+
return nil, errors.New("failed to unmarshal envelope from bytes")
227228
}
228229
cHdr, sDepSpec, err := ccpackage.ExtractSignedCCDepSpec(env)
229230
if err != nil {
230231
return nil, err
231232
}
232233

233234
if cHdr.Type != int32(common.HeaderType_CHAINCODE_PACKAGE) {
234-
return nil, fmt.Errorf("invalid type of envelope for chaincode package")
235+
return nil, errors.New("invalid type of envelope for chaincode package")
235236
}
236237

237238
depSpec := &pb.ChaincodeDeploymentSpec{}
238239
err = proto.Unmarshal(sDepSpec.ChaincodeDeploymentSpec, depSpec)
239240
if err != nil {
240-
return nil, fmt.Errorf("error getting deployment spec")
241+
return nil, errors.New("error getting deployment spec")
241242
}
242243

243244
databytes, id, data, err := ccpack.getCDSData(sDepSpec)
@@ -278,27 +279,27 @@ func (ccpack *SignedCDSPackage) InitFromPath(ccNameVersion string, path string)
278279
// PutChaincodeToFS - serializes chaincode to a package on the file system
279280
func (ccpack *SignedCDSPackage) PutChaincodeToFS() error {
280281
if ccpack.buf == nil {
281-
return fmt.Errorf("uninitialized package")
282+
return errors.New("uninitialized package")
282283
}
283284

284285
if ccpack.id == nil {
285-
return fmt.Errorf("id cannot be nil if buf is not nil")
286+
return errors.New("id cannot be nil if buf is not nil")
286287
}
287288

288289
if ccpack.sDepSpec == nil || ccpack.depSpec == nil {
289-
return fmt.Errorf("depspec cannot be nil if buf is not nil")
290+
return errors.New("depspec cannot be nil if buf is not nil")
290291
}
291292

292293
if ccpack.env == nil {
293-
return fmt.Errorf("env cannot be nil if buf and depspec are not nil")
294+
return errors.New("env cannot be nil if buf and depspec are not nil")
294295
}
295296

296297
if ccpack.data == nil {
297-
return fmt.Errorf("nil data")
298+
return errors.New("nil data")
298299
}
299300

300301
if ccpack.datab == nil {
301-
return fmt.Errorf("nil data bytes")
302+
return errors.New("nil data bytes")
302303
}
303304

304305
ccname := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Name

core/config/configtest/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func gopathDevConfigDir() (string, error) {
6767
}
6868
}
6969

70-
return "", fmt.Errorf("unable to find sampleconfig directory on GOPATH")
70+
return "", errors.New("unable to find sampleconfig directory on GOPATH")
7171
}
7272

7373
func gomodDevConfigDir() (string, error) {

core/ledger/kvledger/benchmark/chainmgmt/chains.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package chainmgmt
88

99
import (
10+
"errors"
1011
"fmt"
1112
"os"
1213
"path/filepath"
@@ -85,7 +86,7 @@ func (m *chainsMgr) createOrOpenChains() []*Chain {
8586
}
8687

8788
default:
88-
panic(fmt.Errorf("unknown chain init operation"))
89+
panic(errors.New("unknown chain init operation"))
8990
}
9091
return m.chains()
9192
}
@@ -147,7 +148,7 @@ func (c *Chain) Done() {
147148
// Commit overrides the Commit function in ledger.PeerLedger because,
148149
// experiments are not expected to call Commit directly to the ledger
149150
func (c *Chain) Commit(block *common.Block) {
150-
panic(fmt.Errorf("Commit should not be invoked directly"))
151+
panic(errors.New("Commit should not be invoked directly"))
151152
}
152153

153154
func (c *Chain) close() {

core/peer/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (c *Config) load() error {
301301
c.ExternalBuilders = externalBuilders
302302
for builderIndex, builder := range c.ExternalBuilders {
303303
if builder.Path == "" {
304-
return fmt.Errorf("invalid external builder configuration, path attribute missing in one or more builders")
304+
return errors.New("invalid external builder configuration, path attribute missing in one or more builders")
305305
}
306306
if builder.Name == "" {
307307
return fmt.Errorf("external builder at path %s has no name attribute", builder.Path)
@@ -338,7 +338,7 @@ func (c *Config) load() error {
338338
func getLocalAddress() (string, error) {
339339
peerAddress := viper.GetString("peer.address")
340340
if peerAddress == "" {
341-
return "", fmt.Errorf("peer.address isn't set")
341+
return "", errors.New("peer.address isn't set")
342342
}
343343
host, port, err := net.SplitHostPort(peerAddress)
344344
if err != nil {

core/peer/configtx_processor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package peer
88

99
import (
10+
"errors"
1011
"fmt"
1112

1213
"github.com/hyperledger/fabric-protos-go-apiv2/common"
@@ -34,7 +35,7 @@ func (tp *ConfigTxProcessor) GenerateSimulationResults(txEnv *common.Envelope, s
3435
case common.HeaderType_CONFIG:
3536
peerLogger.Debugf("Processing CONFIG")
3637
if payload.Data == nil {
37-
return fmt.Errorf("channel config found nil")
38+
return errors.New("channel config found nil")
3839
}
3940
return simulator.SetState(peerNamespace, channelConfigKey, payload.Data)
4041
default:

protoutil/blockutils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func searchConsenterIdentityByID(consenters []*cb.Consenter, identifier uint32)
309309

310310
func VerifyTransactionsAreWellFormed(bd *cb.BlockData) error {
311311
if bd == nil || bd.Data == nil || len(bd.Data) == 0 {
312-
return fmt.Errorf("empty block")
312+
return errors.New("empty block")
313313
}
314314

315315
// If we have a single transaction, and the block is a config block, then no need to check

protoutil/signeddata.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"crypto/x509"
1212
"encoding/pem"
13+
"errors"
1314
"fmt"
1415
"strings"
1516

@@ -33,7 +34,7 @@ type SignedData struct {
3334
// possible.
3435
func ConfigUpdateEnvelopeAsSignedData(ce *common.ConfigUpdateEnvelope) ([]*SignedData, error) {
3536
if ce == nil {
36-
return nil, fmt.Errorf("No signatures for nil SignedConfigItem")
37+
return nil, errors.New("No signatures for nil SignedConfigItem")
3738
}
3839

3940
result := make([]*SignedData, len(ce.Signatures))
@@ -59,7 +60,7 @@ func ConfigUpdateEnvelopeAsSignedData(ce *common.ConfigUpdateEnvelope) ([]*Signe
5960
// slice of length 1 or an error indicating why this was not possible.
6061
func EnvelopeAsSignedData(env *common.Envelope) ([]*SignedData, error) {
6162
if env == nil {
62-
return nil, fmt.Errorf("No signatures for nil Envelope")
63+
return nil, errors.New("No signatures for nil Envelope")
6364
}
6465

6566
payload := &common.Payload{}
@@ -69,7 +70,7 @@ func EnvelopeAsSignedData(env *common.Envelope) ([]*SignedData, error) {
6970
}
7071

7172
if payload.Header == nil /* || payload.Header.SignatureHeader == nil */ {
72-
return nil, fmt.Errorf("Missing Header")
73+
return nil, errors.New("Missing Header")
7374
}
7475

7576
shdr := &common.SignatureHeader{}

0 commit comments

Comments
 (0)