From 0e2784cd2cd329dfb6de12c086292cbf03a10a8d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 6 Aug 2020 19:09:57 +0300 Subject: [PATCH] always wrap errors when creating new ones with fmt.Errorf() It doesn't really change anything in most of the cases, but it's a useful habit anyway. Fix #350. --- cli/server/server.go | 12 ++++++------ cli/smartcontract/smart_contract.go | 20 ++++++++++---------- cli/wallet/multisig.go | 14 +++++++------- cli/wallet/nep5.go | 20 ++++++++++---------- cli/wallet/wallet.go | 12 ++++++------ docs/conventions.md | 23 ++++++++++++++++++++++- pkg/compiler/compiler.go | 6 +++--- pkg/core/blockchain.go | 10 +++++----- pkg/core/interop/crypto/ecdsa.go | 4 ++-- pkg/core/interop_neo.go | 14 +++++++------- pkg/core/native/interop.go | 2 +- pkg/core/native/native_gas.go | 2 +- pkg/core/storage/boltdb_store.go | 2 +- pkg/io/fileWriter.go | 2 +- pkg/network/tcp_peer.go | 2 +- pkg/rpc/client/nep5.go | 12 ++++++------ pkg/rpc/client/rpc_test.go | 4 ++-- pkg/rpc/request/types.go | 4 ++-- pkg/rpc/server/server.go | 2 +- pkg/smartcontract/nef/nef.go | 8 ++++---- pkg/vm/cli/cli.go | 4 ++-- pkg/vm/interop.go | 2 +- pkg/vm/json_test.go | 2 +- scripts/compare-dumps.go | 6 +++--- 24 files changed, 105 insertions(+), 84 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index ef58f135a..cc83178f3 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -197,7 +197,7 @@ func dumpDB(ctx *cli.Context) error { bh := chain.GetHeaderHash(int(i)) b, err := chain.GetBlock(bh) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to get block %d: %s", i, err), 1) + return cli.NewExitError(fmt.Errorf("failed to get block %d: %w", i, err), 1) } buf := io.NewBufBinWriter() b.EncodeBinary(buf.BinWriter) @@ -295,7 +295,7 @@ func restoreDB(ctx *cli.Context) error { } else { err = chain.AddBlock(block) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1) + return cli.NewExitError(fmt.Errorf("failed to add block %d: %w", i, err), 1) } } if dumpDir != "" { @@ -308,7 +308,7 @@ func restoreDB(ctx *cli.Context) error { lastIndex = block.Index if block.Index%1000 == 0 { if err := dump.tryPersist(dumpDir, block.Index); err != nil { - return cli.NewExitError(fmt.Errorf("can't dump storage to file: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't dump storage to file: %w", err), 1) } } } @@ -349,7 +349,7 @@ func startServer(ctx *cli.Context) error { serv, err := network.NewServer(serverConfig, chain, log) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to create network server: %v", err), 1) + return cli.NewExitError(fmt.Errorf("failed to create network server: %w", err), 1) } rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, serv, log) errChan := make(chan error) @@ -410,12 +410,12 @@ func configureAddresses(cfg config.ApplicationConfiguration) { func initBlockChain(cfg config.Config, log *zap.Logger) (*core.Blockchain, error) { store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration) if err != nil { - return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %s", err), 1) + return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %w", err), 1) } chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration, log) if err != nil { - return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %s", err), 1) + return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %w", err), 1) } return chain, nil } diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index ad590b6f9..a6f62960e 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -411,7 +411,7 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error { } script, err := util.Uint160DecodeStringLE(args[0]) if err != nil { - return cli.NewExitError(fmt.Errorf("incorrect script hash: %v", err), 1) + return cli.NewExitError(fmt.Errorf("incorrect script hash: %w", err), 1) } if len(args) <= 1 { return cli.NewExitError(errNoMethod, 1) @@ -427,7 +427,7 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error { } param, err := smartcontract.NewParameterFromString(s) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to parse argument #%d: %v", k+paramsStart+1, err), 1) + return cli.NewExitError(fmt.Errorf("failed to parse argument #%d: %w", k+paramsStart+1, err), 1) } params = append(params, *param) } @@ -437,7 +437,7 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error { for i, c := range args[cosignersStart:] { cosigner, err := parseCosigner(c) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to parse cosigner #%d: %v", i+1, err), 1) + return cli.NewExitError(fmt.Errorf("failed to parse cosigner #%d: %w", i+1, err), 1) } cosigners = append(cosigners, cosigner) } @@ -468,11 +468,11 @@ func invokeInternal(ctx *cli.Context, signAndPush bool) error { } script, err := hex.DecodeString(resp.Script) if err != nil { - return cli.NewExitError(fmt.Errorf("bad script returned from the RPC node: %v", err), 1) + return cli.NewExitError(fmt.Errorf("bad script returned from the RPC node: %w", err), 1) } txHash, err := c.SignAndPushInvocationTx(script, acc, resp.GasConsumed, gas, cosigners) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1) + return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1) } fmt.Printf("Sent invocation transaction %s\n", txHash.StringLE()) } else { @@ -508,7 +508,7 @@ func testInvokeScript(ctx *cli.Context) error { for i, c := range args[:] { cosigner, err := parseCosigner(c) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to parse signer #%d: %v", i+1, err), 1) + return cli.NewExitError(fmt.Errorf("failed to parse signer #%d: %w", i+1, err), 1) } signers = append(signers, cosigner) } @@ -668,17 +668,17 @@ func contractDeploy(ctx *cli.Context) error { txScript, err := request.CreateDeploymentScript(nefFile.Script, m) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to create deployment script: %v", err), 1) + return cli.NewExitError(fmt.Errorf("failed to create deployment script: %w", err), 1) } // It doesn't require any signers. invRes, err := c.InvokeScript(txScript, nil) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to test-invoke deployment script: %v", err), 1) + return cli.NewExitError(fmt.Errorf("failed to test-invoke deployment script: %w", err), 1) } txHash, err := c.SignAndPushInvocationTx(txScript, acc, invRes.GasConsumed, gas, nil) if err != nil { - return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1) + return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1) } fmt.Printf("Sent deployment transaction %s for contract %s\n", txHash.StringLE(), nefFile.Header.ScriptHash.StringLE()) return nil @@ -693,7 +693,7 @@ func parseContractConfig(confFile string) (ProjectConfig, error) { err = yaml.Unmarshal(confBytes, &conf) if err != nil { - return conf, cli.NewExitError(fmt.Errorf("bad config: %v", err), 1) + return conf, cli.NewExitError(fmt.Errorf("bad config: %w", err), 1) } return conf, nil } diff --git a/cli/wallet/multisig.go b/cli/wallet/multisig.go index 3165de97f..0fde124aa 100644 --- a/cli/wallet/multisig.go +++ b/cli/wallet/multisig.go @@ -48,7 +48,7 @@ func signMultisig(ctx *cli.Context) error { addr := ctx.String("addr") sh, err := address.StringToUint160(addr) if err != nil { - return cli.NewExitError(fmt.Errorf("invalid address: %v", err), 1) + return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) } acc := wall.GetAccount(sh) if acc == nil { @@ -65,13 +65,13 @@ func signMultisig(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } else if err := acc.Decrypt(pass); err != nil { - return cli.NewExitError(fmt.Errorf("can't unlock an account: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't unlock an account: %w", err), 1) } priv := acc.PrivateKey() sign := priv.Sign(tx.GetSignedPart()) if err := c.AddSignature(acc.Contract, priv.PublicKey(), sign); err != nil { - return cli.NewExitError(fmt.Errorf("can't add signature: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't add signature: %w", err), 1) } else if err := writeParameterContext(c, ctx.String("out")); err != nil { return cli.NewExitError(err, 1) } @@ -104,21 +104,21 @@ func signMultisig(ctx *cli.Context) error { func readParameterContext(filename string) (*context.ParameterContext, error) { data, err := ioutil.ReadFile(filename) if err != nil { - return nil, fmt.Errorf("can't read input file: %v", err) + return nil, fmt.Errorf("can't read input file: %w", err) } c := new(context.ParameterContext) if err := json.Unmarshal(data, c); err != nil { - return nil, fmt.Errorf("can't parse transaction: %v", err) + return nil, fmt.Errorf("can't parse transaction: %w", err) } return c, nil } func writeParameterContext(c *context.ParameterContext, filename string) error { if data, err := json.Marshal(c); err != nil { - return fmt.Errorf("can't marshal transaction: %v", err) + return fmt.Errorf("can't marshal transaction: %w", err) } else if err := ioutil.WriteFile(filename, data, 0644); err != nil { - return fmt.Errorf("can't write transaction to file: %v", err) + return fmt.Errorf("can't write transaction to file: %w", err) } return nil } diff --git a/cli/wallet/nep5.go b/cli/wallet/nep5.go index 51fa47600..f41a4ad4a 100644 --- a/cli/wallet/nep5.go +++ b/cli/wallet/nep5.go @@ -144,7 +144,7 @@ func getNEP5Balance(ctx *cli.Context) error { addr := ctx.String("addr") addrHash, err := address.StringToUint160(addr) if err != nil { - return cli.NewExitError(fmt.Errorf("invalid address: %v", err), 1) + return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1) } acc := wall.GetAccount(addrHash) if acc == nil { @@ -242,7 +242,7 @@ func importNEP5Token(ctx *cli.Context) error { tokenHash, err := util.Uint160DecodeStringLE(ctx.String("token")) if err != nil { - return cli.NewExitError(fmt.Errorf("invalid token contract hash: %v", err), 1) + return cli.NewExitError(fmt.Errorf("invalid token contract hash: %w", err), 1) } for _, t := range wall.Extra.Tokens { @@ -262,7 +262,7 @@ func importNEP5Token(ctx *cli.Context) error { tok, err := c.NEP5TokenInfo(tokenHash) if err != nil { - return cli.NewExitError(fmt.Errorf("can't receive token info: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't receive token info: %w", err), 1) } wall.AddToken(tok) @@ -327,9 +327,9 @@ func removeNEP5Token(ctx *cli.Context) error { } } if err := wall.RemoveToken(token.Hash); err != nil { - return cli.NewExitError(fmt.Errorf("can't remove token: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't remove token: %w", err), 1) } else if err := wall.Save(); err != nil { - return cli.NewExitError(fmt.Errorf("error while saving wallet: %v", err), 1) + return cli.NewExitError(fmt.Errorf("error while saving wallet: %w", err), 1) } return nil } @@ -385,7 +385,7 @@ func multiTransferNEP5(ctx *cli.Context) error { } amount, err := util.FixedNFromString(ss[2], int(token.Decimals)) if err != nil { - return cli.NewExitError(fmt.Errorf("invalid amount: %v", err), 1) + return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1) } recepients = append(recepients, client.TransferTarget{ Token: token.Hash, @@ -432,7 +432,7 @@ func transferNEP5(ctx *cli.Context) error { amount, err := util.FixedNFromString(ctx.String("amount"), int(token.Decimals)) if err != nil { - return cli.NewExitError(fmt.Errorf("invalid amount: %v", err), 1) + return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1) } return signAndSendTransfer(ctx, c, acc, []client.TransferTarget{{ @@ -464,11 +464,11 @@ func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account sign := priv.Sign(tx.GetSignedPart()) scCtx := context.NewParameterContext("Neo.Core.ContractTransaction", tx) if err := scCtx.AddSignature(acc.Contract, pub, sign); err != nil { - return cli.NewExitError(fmt.Errorf("can't add signature: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't add signature: %w", err), 1) } else if data, err := json.Marshal(scCtx); err != nil { - return cli.NewExitError(fmt.Errorf("can't marshal tx to JSON: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't marshal tx to JSON: %w", err), 1) } else if err := ioutil.WriteFile(outFile, data, 0644); err != nil { - return cli.NewExitError(fmt.Errorf("can't write tx to file: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't write tx to file: %w", err), 1) } } else { _ = acc.SignTx(tx) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 91ddb6847..4c89bd43a 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -262,7 +262,7 @@ func convertWallet(ctx *cli.Context) error { address.Prefix = address.NEO3Prefix newAcc, err := wallet.NewAccountFromWIF(acc.PrivateKey().WIF()) if err != nil { - return cli.NewExitError(fmt.Errorf("can't convert account: %v", err), -1) + return cli.NewExitError(fmt.Errorf("can't convert account: %w", err), -1) } newAcc.Address = address.Uint160ToString(acc.Contract.ScriptHash()) newAcc.Contract = acc.Contract @@ -270,7 +270,7 @@ func convertWallet(ctx *cli.Context) error { newAcc.Label = acc.Label newAcc.Locked = acc.Locked if err := newAcc.Encrypt(pass); err != nil { - return cli.NewExitError(fmt.Errorf("can't encrypt converted account: %v", err), -1) + return cli.NewExitError(fmt.Errorf("can't encrypt converted account: %w", err), -1) } newWallet.AddAccount(newAcc) } @@ -311,7 +311,7 @@ func exportKeys(ctx *cli.Context) error { addr = ctx.Args().First() _, err := address.StringToUint160(addr) if err != nil { - return cli.NewExitError(fmt.Errorf("can't parse address: %v", err), 1) + return cli.NewExitError(fmt.Errorf("can't parse address: %w", err), 1) } } @@ -372,7 +372,7 @@ func importMultisig(ctx *cli.Context) error { for i := range args { pubs[i], err = keys.NewPublicKeyFromString(args[i]) if err != nil { - return cli.NewExitError(fmt.Errorf("can't decode public key %d: %v", i, err), 1) + return cli.NewExitError(fmt.Errorf("can't decode public key %d: %w", i, err), 1) } } @@ -446,9 +446,9 @@ func removeAccount(ctx *cli.Context) error { } if err := wall.RemoveAccount(acc.Address); err != nil { - return cli.NewExitError(fmt.Errorf("error on remove: %v", err), 1) + return cli.NewExitError(fmt.Errorf("error on remove: %w", err), 1) } else if err := wall.Save(); err != nil { - return cli.NewExitError(fmt.Errorf("error while saving wallet: %v", err), 1) + return cli.NewExitError(fmt.Errorf("error while saving wallet: %w", err), 1) } return nil } diff --git a/docs/conventions.md b/docs/conventions.md index e5a6f5eab..364c791d2 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -14,4 +14,25 @@ func example(test int) (num int) { return } -In the above function we have used a named return paramter, which allows you to include a simple return statement without the variables you are returning. This practice can cause confusion when functions become large or the logic becomes complex, so these should be avoided. \ No newline at end of file +In the above function we have used a named return paramter, which allows you to include a simple return statement without the variables you are returning. This practice can cause confusion when functions become large or the logic becomes complex, so these should be avoided. + +## Use error wrapping + +Bad: +``` +err = SomeAPI() +if err != nil { + return fmt.Errorf("something bad happened: %v", err) +} +``` + +Good: +``` +err = SomeAPI() +if err != nil { + return fmt.Errorf("something bad happened: %w", err) +} +``` + +Error wrapping allows `errors.Is` and `errors.As` usage in upper layer +functions which might be useful. diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 19a00b056..b221b8d74 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -140,15 +140,15 @@ func CompileAndSave(src string, o *Options) ([]byte, error) { } b, di, err := CompileWithDebugInfo(bytes.NewReader(b)) if err != nil { - return nil, fmt.Errorf("error while trying to compile smart contract file: %v", err) + return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err) } f, err := nef.NewFile(b) if err != nil { - return nil, fmt.Errorf("error while trying to create .nef file: %v", err) + return nil, fmt.Errorf("error while trying to create .nef file: %w", err) } bytes, err := f.Bytes() if err != nil { - return nil, fmt.Errorf("error while serializing .nef file: %v", err) + return nil, fmt.Errorf("error while serializing .nef file: %w", err) } out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext) err = ioutil.WriteFile(out, bytes, os.ModePerm) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 3cd33e991..bfbdbba60 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -263,7 +263,7 @@ func (bc *Blockchain) init() error { for hash != targetHash { header, err := bc.GetHeader(hash) if err != nil { - return fmt.Errorf("could not get header %s: %s", hash, err) + return fmt.Errorf("could not get header %s: %w", hash, err) } headers = append(headers, header) hash = header.PrevHash @@ -433,13 +433,13 @@ func (bc *Blockchain) AddBlock(block *block.Block) error { if bc.config.VerifyBlocks { err := block.Verify() if err != nil { - return fmt.Errorf("block %s is invalid: %s", block.Hash().StringLE(), err) + return fmt.Errorf("block %s is invalid: %w", block.Hash().StringLE(), err) } if bc.config.VerifyTransactions { for _, tx := range block.Transactions { err := bc.VerifyTx(tx, block) if err != nil { - return fmt.Errorf("transaction %s failed to verify: %s", tx.Hash().StringLE(), err) + return fmt.Errorf("transaction %s failed to verify: %w", tx.Hash().StringLE(), err) } } } @@ -478,7 +478,7 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) (err err // Verify that the chain of the headers is consistent. var lastHeader *block.Header if lastHeader, err = bc.GetHeader(headers[0].PrevHash); err != nil { - return fmt.Errorf("previous header was not found: %v", err) + return fmt.Errorf("previous header was not found: %w", err) } for _, h := range headers { if err = bc.verifyHeader(h, lastHeader); err != nil { @@ -1473,7 +1473,7 @@ func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transa } err = vm.Run() if vm.HasFailed() { - return fmt.Errorf("vm failed to execute the script with error: %s", err) + return fmt.Errorf("vm execution has failed: %w", err) } resEl := vm.Estack().Pop() if resEl != nil { diff --git a/pkg/core/interop/crypto/ecdsa.go b/pkg/core/interop/crypto/ecdsa.go index 67676292c..333cc63ff 100644 --- a/pkg/core/interop/crypto/ecdsa.go +++ b/pkg/core/interop/crypto/ecdsa.go @@ -62,14 +62,14 @@ func ecdsaCheckMultisig(ic *interop.Context, v *vm.VM, curve elliptic.Curve) err hashToCheck := hash.Sha256(msg).BytesBE() pkeys, err := v.Estack().PopSigElements() if err != nil { - return fmt.Errorf("wrong parameters: %s", err.Error()) + return fmt.Errorf("wrong parameters: %w", err) } if !v.AddGas(ECDSAVerifyPrice * int64(len(pkeys))) { return errors.New("gas limit exceeded") } sigs, err := v.Estack().PopSigElements() if err != nil { - return fmt.Errorf("wrong parameters: %s", err.Error()) + return fmt.Errorf("wrong parameters: %w", err) } // It's ok to have more keys than there are signatures (it would // just mean that some keys didn't sign), but not the other way around. diff --git a/pkg/core/interop_neo.go b/pkg/core/interop_neo.go index 5fa5d3067..cb1588b9d 100644 --- a/pkg/core/interop_neo.go +++ b/pkg/core/interop_neo.go @@ -74,7 +74,7 @@ func createContractStateFromVM(ic *interop.Context, v *vm.VM) (*state.Contract, var m manifest.Manifest err := m.UnmarshalJSON(manifestBytes) if err != nil { - return nil, fmt.Errorf("unable to retrieve manifest from stack: %v", err) + return nil, fmt.Errorf("unable to retrieve manifest from stack: %w", err) } return &state.Contract{ Script: script, @@ -105,7 +105,7 @@ func contractCreate(ic *interop.Context, v *vm.VM) error { } cs, err := contractToStackItem(newcontract) if err != nil { - return fmt.Errorf("cannot convert contract to stack item: %v", err) + return fmt.Errorf("cannot convert contract to stack item: %w", err) } v.Estack().PushVal(cs) return nil @@ -149,10 +149,10 @@ func contractUpdate(ic *interop.Context, v *vm.VM) error { } contract.Manifest.ABI.Hash = newHash if err := ic.DAO.PutContractState(contract); err != nil { - return fmt.Errorf("failed to update script: %v", err) + return fmt.Errorf("failed to update script: %w", err) } if err := ic.DAO.DeleteContractState(oldHash); err != nil { - return fmt.Errorf("failed to update script: %v", err) + return fmt.Errorf("failed to update script: %w", err) } } // if manifest was provided, update the old contract manifest and check associated @@ -161,7 +161,7 @@ func contractUpdate(ic *interop.Context, v *vm.VM) error { var newManifest manifest.Manifest err := newManifest.UnmarshalJSON(manifestBytes) if err != nil { - return fmt.Errorf("unable to retrieve manifest from stack: %v", err) + return fmt.Errorf("unable to retrieve manifest from stack: %w", err) } // we don't have to perform `GetContractState` one more time as it's already up-to-date contract.Manifest = newManifest @@ -171,14 +171,14 @@ func contractUpdate(ic *interop.Context, v *vm.VM) error { if !contract.HasStorage() { siMap, err := ic.DAO.GetStorageItems(contract.ID) if err != nil { - return fmt.Errorf("failed to update manifest: %v", err) + return fmt.Errorf("failed to update manifest: %w", err) } if len(siMap) != 0 { return errors.New("old contract shouldn't have storage") } } if err := ic.DAO.PutContractState(contract); err != nil { - return fmt.Errorf("failed to update manifest: %v", err) + return fmt.Errorf("failed to update manifest: %w", err) } } diff --git a/pkg/core/native/interop.go b/pkg/core/native/interop.go index 5e1d23515..cdbed93cc 100644 --- a/pkg/core/native/interop.go +++ b/pkg/core/native/interop.go @@ -27,7 +27,7 @@ func Deploy(ic *interop.Context, _ *vm.VM) error { return err } if err := native.Initialize(ic); err != nil { - return fmt.Errorf("initializing %s native contract: %v", md.Name, err) + return fmt.Errorf("initializing %s native contract: %w", md.Name, err) } } return nil diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index cca7655bd..8c524b77c 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -92,7 +92,7 @@ func (g *GAS) OnPersist(ic *interop.Context) error { } validators, err := g.NEO.getNextBlockValidatorsInternal(ic.Chain, ic.DAO) if err != nil { - return fmt.Errorf("cannot get block validators: %v", err) + return fmt.Errorf("can't get block validators: %w", err) } primary := validators[ic.Block.ConsensusData.PrimaryIndex].GetScriptHash() var netFee int64 diff --git a/pkg/core/storage/boltdb_store.go b/pkg/core/storage/boltdb_store.go index fa906ce06..feb0718ea 100644 --- a/pkg/core/storage/boltdb_store.go +++ b/pkg/core/storage/boltdb_store.go @@ -39,7 +39,7 @@ func NewBoltDBStore(cfg BoltDBOptions) (*BoltDBStore, error) { err = db.Update(func(tx *bbolt.Tx) error { _, err = tx.CreateBucketIfNotExists(Bucket) if err != nil { - return fmt.Errorf("could not create root bucket: %v", err) + return fmt.Errorf("could not create root bucket: %w", err) } return nil }) diff --git a/pkg/io/fileWriter.go b/pkg/io/fileWriter.go index 35d87f34f..35f3e7c62 100644 --- a/pkg/io/fileWriter.go +++ b/pkg/io/fileWriter.go @@ -12,7 +12,7 @@ func MakeDirForFile(filePath string, creator string) error { dir := path.Dir(fileName) err := os.MkdirAll(dir, os.ModePerm) if err != nil { - return fmt.Errorf("could not create dir for %s: %v", creator, err) + return fmt.Errorf("could not create dir for %s: %w", creator, err) } return nil } diff --git a/pkg/network/tcp_peer.go b/pkg/network/tcp_peer.go index 664089bdf..aec25796e 100644 --- a/pkg/network/tcp_peer.go +++ b/pkg/network/tcp_peer.go @@ -161,7 +161,7 @@ func (p *TCPPeer) handleConn() { } if err = p.server.handleMessage(p, msg); err != nil { if p.Handshaked() { - err = fmt.Errorf("handling %s message: %v", msg.Command.String(), err) + err = fmt.Errorf("handling %s message: %w", msg.Command.String(), err) } break } diff --git a/pkg/rpc/client/nep5.go b/pkg/rpc/client/nep5.go index 5c3ea9304..384305fc8 100644 --- a/pkg/rpc/client/nep5.go +++ b/pkg/rpc/client/nep5.go @@ -123,7 +123,7 @@ func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, toke func (c *Client) CreateNEP5MultiTransferTx(acc *wallet.Account, gas int64, recepients ...TransferTarget) (*transaction.Transaction, error) { from, err := address.StringToUint160(acc.Address) if err != nil { - return nil, fmt.Errorf("bad account address: %v", err) + return nil, fmt.Errorf("bad account address: %w", err) } w := io.NewBufBinWriter() for i := range recepients { @@ -140,7 +140,7 @@ func (c *Client) CreateNEP5MultiTransferTx(acc *wallet.Account, gas int64, recep }, }) if err != nil { - return nil, fmt.Errorf("can't add system fee to transaction: %v", err) + return nil, fmt.Errorf("can't add system fee to transaction: %w", err) } tx := transaction.New(c.opts.Network, script, result.GasConsumed) tx.Signers = []transaction.Signer{ @@ -151,12 +151,12 @@ func (c *Client) CreateNEP5MultiTransferTx(acc *wallet.Account, gas int64, recep } tx.ValidUntilBlock, err = c.CalculateValidUntilBlock() if err != nil { - return nil, fmt.Errorf("can't calculate validUntilBlock: %v", err) + return nil, fmt.Errorf("can't calculate validUntilBlock: %w", err) } err = c.AddNetworkFee(tx, gas, acc) if err != nil { - return nil, fmt.Errorf("can't add network fee to transaction: %v", err) + return nil, fmt.Errorf("can't add network fee to transaction: %w", err) } return tx, nil @@ -173,7 +173,7 @@ func (c *Client) TransferNEP5(acc *wallet.Account, to util.Uint160, token util.U } if err := acc.SignTx(tx); err != nil { - return util.Uint256{}, fmt.Errorf("can't sign tx: %v", err) + return util.Uint256{}, fmt.Errorf("can't sign tx: %w", err) } return c.SendRawTransaction(tx) @@ -187,7 +187,7 @@ func (c *Client) MultiTransferNEP5(acc *wallet.Account, gas int64, recepients .. } if err := acc.SignTx(tx); err != nil { - return util.Uint256{}, fmt.Errorf("can't sign tx: %v", err) + return util.Uint256{}, fmt.Errorf("can't sign tx: %w", err) } return c.SendRawTransaction(tx) diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index 4f83d7922..ca1265277 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -690,7 +690,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ result: func(c *Client) interface{} { h, err := util.Uint256DecodeStringLE("72159b0cf1221110daad6e1df6ef4ff03012173b63c86910bd7134deb659c875") if err != nil { - panic(fmt.Errorf("can't decode `sendrawtransaction` result hash: %v", err)) + panic(fmt.Errorf("can't decode `sendrawtransaction` result hash: %w", err)) } return h }, @@ -710,7 +710,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ result: func(c *Client) interface{} { h, err := util.Uint256DecodeStringLE("1bdea8f80eb5bd97fade38d5e7fb93b02c9d3e01394e9f4324218132293f7ea6") if err != nil { - panic(fmt.Errorf("can't decode `submitblock` result hash: %v", err)) + panic(fmt.Errorf("can't decode `submitblock` result hash: %w", err)) } return h }, diff --git a/pkg/rpc/request/types.go b/pkg/rpc/request/types.go index 4da21d45b..6882385f1 100644 --- a/pkg/rpc/request/types.go +++ b/pkg/rpc/request/types.go @@ -59,7 +59,7 @@ func (r *In) DecodeData(data io.ReadCloser) error { err := json.NewDecoder(data).Decode(r) if err != nil { - return fmt.Errorf("error parsing JSON payload: %s", err) + return fmt.Errorf("error parsing JSON payload: %w", err) } if r.JSONRPC != JSONRPCVersion { @@ -76,7 +76,7 @@ func (r *In) Params() (*Params, error) { err := json.Unmarshal(r.RawParams, ¶ms) if err != nil { - return nil, fmt.Errorf("error parsing params field in payload: %s", err) + return nil, fmt.Errorf("error parsing params: %w", err) } return ¶ms, nil diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index c64e48e54..d7407aad7 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -614,7 +614,7 @@ func (s *Server) getDecimals(contractID int32, cache map[int32]decimals) (decima }, }) if err != nil { - return decimals{}, fmt.Errorf("can't create script: %v", err) + return decimals{}, fmt.Errorf("can't create script: %w", err) } res := s.runScriptInVM(script, nil) if res == nil || res.State != "HALT" || len(res.Stack) == 0 { diff --git a/pkg/smartcontract/nef/nef.go b/pkg/smartcontract/nef/nef.go index 7ab79aae9..9725e3df4 100644 --- a/pkg/smartcontract/nef/nef.go +++ b/pkg/smartcontract/nef/nef.go @@ -95,13 +95,13 @@ func GetVersion(version string) (Version, error) { } major, err := strconv.ParseInt(versions[0], 10, 32) if err != nil { - return result, fmt.Errorf("failed to parse major version: %v", err) + return result, fmt.Errorf("failed to parse major version: %w", err) } result.Major = int32(major) minor, err := strconv.ParseInt(versions[1], 10, 32) if err != nil { - return result, fmt.Errorf("failed to parse minor version: %v", err) + return result, fmt.Errorf("failed to parse minor version: %w", err) } result.Minor = int32(minor) @@ -112,7 +112,7 @@ func GetVersion(version string) (Version, error) { } build, err := strconv.ParseInt(b, 10, 32) if err != nil { - return result, fmt.Errorf("failed to parse build version: %v", err) + return result, fmt.Errorf("failed to parse build version: %w", err) } result.Build = int32(build) @@ -120,7 +120,7 @@ func GetVersion(version string) (Version, error) { r := strings.SplitN(versions[3], "-", 2)[0] revision, err := strconv.ParseInt(r, 10, 32) if err != nil { - return result, fmt.Errorf("failed to parse revision version: %v", err) + return result, fmt.Errorf("failed to parse revision version: %w", err) } result.Revision = int32(revision) } diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 1ec35e2e0..c2109c6c6 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -241,7 +241,7 @@ func handleBreak(c *ishell.Context) { } n, err := strconv.Atoi(c.Args[0]) if err != nil { - c.Err(fmt.Errorf("argument conversion error: %s", err)) + c.Err(fmt.Errorf("argument conversion error: %w", err)) return } @@ -388,7 +388,7 @@ func handleStep(c *ishell.Context) { if len(c.Args) > 0 { n, err = strconv.Atoi(c.Args[0]) if err != nil { - c.Err(fmt.Errorf("argument conversion error: %s", err)) + c.Err(fmt.Errorf("argument conversion error: %w", err)) return } } diff --git a/pkg/vm/interop.go b/pkg/vm/interop.go index 1d68c46aa..ec1cf0ef3 100644 --- a/pkg/vm/interop.go +++ b/pkg/vm/interop.go @@ -128,7 +128,7 @@ func EnumeratorCreate(v *VM) error { default: data, err := t.TryBytes() if err != nil { - return fmt.Errorf("can not create enumerator from type %s: %v", t.Type(), err) + return fmt.Errorf("can not create enumerator from type %s: %w", t.Type(), err) } interop = &byteArrayWrapper{ index: -1, diff --git a/pkg/vm/json_test.go b/pkg/vm/json_test.go index 0f0057f41..36fb7660a 100644 --- a/pkg/vm/json_test.go +++ b/pkg/vm/json_test.go @@ -437,7 +437,7 @@ func (v *vmUTStackItem) UnmarshalJSON(data []byte) error { var it vmUTStackItem if err := d.Decode(&it); err != nil { - return fmt.Errorf("can't decode map value: %v", err) + return fmt.Errorf("can't decode map value: %w", err) } item := jsonStringToInteger(key) diff --git a/scripts/compare-dumps.go b/scripts/compare-dumps.go index 268806e6d..f3ca4e48c 100644 --- a/scripts/compare-dumps.go +++ b/scripts/compare-dumps.go @@ -55,11 +55,11 @@ func (d dump) normalize() { func compare(a, b string) error { dumpA, err := readFile(a) if err != nil { - return fmt.Errorf("reading file %s: %v", a, err) + return fmt.Errorf("reading file %s: %w", a, err) } dumpB, err := readFile(b) if err != nil { - return fmt.Errorf("reading file %s: %v", b, err) + return fmt.Errorf("reading file %s: %w", b, err) } dumpA.normalize() dumpB.normalize() @@ -143,7 +143,7 @@ func cliMain(c *cli.Context) error { bname := filepath.Join(b, fname) err := compare(aname, bname) if err != nil { - return fmt.Errorf("file %s: %v", fname, err) + return fmt.Errorf("file %s: %w", fname, err) } } }