Skip to content

Commit

Permalink
feat: create a wrapper for errors (#3723)
Browse files Browse the repository at this point in the history
* create a wrapper for errors

* use join to wrap 2 errors

* add changelog

* rollback some deps

* rollback more go deps

* rollback go deps

* rollback mod requires

* add comments and rationale

* improve rationale

* ci: add golangci depguard rule for the errors package (#3740)

Co-authored-by: Danilo Pantani <[email protected]>

* remove duplicated import

* change pkg from xerrors to errors

* rollback go.mod

* remove unused pkg

* fix goanalysis_test

* remove unused package

* use errors.Errorf instead fmt.Errorf

---------

Co-authored-by: Pantani <Pantani>
Co-authored-by: Jerónimo Albi <[email protected]>
  • Loading branch information
Pantani and jeronimoalbi authored Dec 13, 2023
1 parent c8c9f8e commit 1b08ba3
Show file tree
Hide file tree
Showing 157 changed files with 504 additions and 416 deletions.
17 changes: 16 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ linters:
enable:
- bodyclose
- decorder
# - depguard
- depguard
- dogsled
- dupword
# - errcheck
Expand Down Expand Up @@ -42,9 +42,24 @@ linters:
- unused
- unparam
- misspell
- forbidigo
# - wrapcheck
# - wsl

linters-settings:
forbidigo:
forbid:
- p: ^fmt\.Errorf$
msg: fmt.Errorf should be replaced by '"github.com/ignite/cli/ignite/pkg/errors"'
depguard:
rules:
main:
deny:
- pkg: "errors"
desc: Should be replaced by '"github.com/ignite/cli/ignite/pkg/errors"'
- pkg: "github.com/pkg/errors"
desc: Should be replaced by '"github.com/ignite/cli/ignite/pkg/errors"'

issues:
max-issues-per-linter: 0
max-same-issues: 0
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- [#3822](https://github.com/ignite/cli/pull/3822) Improve default scaffolded AutoCLI config
- [#3838](https://github.com/ignite/cli/pull/3838) Scaffold chain with Cosmos SDK `v0.50.2`, and bump confix and x/upgrade to latest
- [#3723](https://github.com/ignite/cli/pull/3723) Create a wrapper for errors

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/glow v1.4.1
github.com/charmbracelet/lipgloss v0.6.0
github.com/cockroachdb/errors v1.11.1
github.com/cometbft/cometbft v0.38.2
github.com/cosmos/cosmos-sdk v0.50.1
github.com/cosmos/go-bip39 v1.0.0
Expand Down Expand Up @@ -67,7 +68,6 @@ require (
github.com/nqd/flat v0.2.0
github.com/otiai10/copy v1.14.0
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
github.com/radovskyb/watcher v1.0.7
github.com/rogpeppe/go-internal v1.11.0
github.com/rs/cors v1.10.1
Expand Down Expand Up @@ -162,7 +162,6 @@ require (
github.com/cilium/ebpf v0.9.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20231102162011-844f0582c2eb // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
Expand Down Expand Up @@ -367,6 +366,7 @@ require (
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/polyfloyd/go-errorlint v1.4.5 // indirect
Expand Down
7 changes: 3 additions & 4 deletions ignite/cmd/account_create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ignitecmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/errors"
)

func NewAccountCreate() *cobra.Command {
Expand All @@ -27,12 +26,12 @@ func accountCreateHandler(cmd *cobra.Command, args []string) error {
cosmosaccount.WithHome(getKeyringDir(cmd)),
)
if err != nil {
return fmt.Errorf("unable to create registry: %w", err)
return errors.Errorf("unable to create registry: %w", err)
}

_, mnemonic, err := ca.Create(name)
if err != nil {
return fmt.Errorf("unable to create account: %w", err)
return errors.Errorf("unable to create account: %w", err)
}

cmd.Printf("Account %q created, keep your mnemonic in a secret place:\n\n%s\n", name, mnemonic)
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/account_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/errors"
)

func NewAccountExport() *cobra.Command {
Expand Down Expand Up @@ -36,7 +37,7 @@ func accountExportHandler(cmd *cobra.Command, args []string) error {
}
const minPassLength = 8
if len(passphrase) < minPassLength {
return fmt.Errorf("passphrase must be at least %d characters", minPassLength)
return errors.Errorf("passphrase must be at least %d characters", minPassLength)
}

ca, err := cosmosaccount.New(
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/account_import.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ignitecmd

import (
"errors"
"os"

"github.com/cosmos/go-bip39"
"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cliui/cliquiz"
"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/errors"
)

const flagSecret = "secret"
Expand Down
6 changes: 3 additions & 3 deletions ignite/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ignitecmd

import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v28/ignite/pkg/cosmosgen"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/goanalysis"
"github.com/ignite/cli/v28/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v28/ignite/pkg/xast"
Expand Down Expand Up @@ -239,7 +239,7 @@ func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, ap
session.Println(prefix)
if err := session.AskConfirm(question); err != nil {
if errors.Is(err, promptui.ErrAbort) {
return fmt.Errorf("stopping because config version v%d is required to run the command", chainconfig.LatestVersion)
return errors.Errorf("stopping because config version v%d is required to run the command", chainconfig.LatestVersion)
}

return err
Expand All @@ -260,7 +260,7 @@ func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, ap
}

if err := os.WriteFile(configPath, buf.Bytes(), 0o755); err != nil {
return fmt.Errorf("config file migration failed: %w", err)
return errors.Errorf("config file migration failed: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ignitecmd

import (
"context"
"errors"
"fmt"

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
cliuimodel "github.com/ignite/cli/v28/ignite/pkg/cliui/model"
"github.com/ignite/cli/v28/ignite/pkg/debugger"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/events"
"github.com/ignite/cli/v28/ignite/pkg/xurl"
"github.com/ignite/cli/v28/ignite/services/chain"
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ignitecmd

import (
"context"
"errors"

tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
Expand All @@ -12,6 +11,7 @@ import (
uilog "github.com/ignite/cli/v28/ignite/pkg/cliui/log"
cliuimodel "github.com/ignite/cli/v28/ignite/pkg/cliui/model"
"github.com/ignite/cli/v28/ignite/pkg/cosmosver"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/events"
"github.com/ignite/cli/v28/ignite/services/chain"
)
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
uilog "github.com/ignite/cli/v28/ignite/pkg/cliui/log"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/gitpod"
"github.com/ignite/cli/v28/ignite/pkg/goenv"
"github.com/ignite/cli/v28/ignite/pkg/xgenny"
Expand Down Expand Up @@ -85,7 +86,7 @@ To get started, create a blockchain:

// Load plugins if any
if err := LoadPlugins(ctx, c); err != nil {
return nil, nil, fmt.Errorf("error while loading apps: %w", err)
return nil, nil, errors.Errorf("error while loading apps: %w", err)
}
return c, UnloadPlugins, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/ignite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"sync"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/clictx"
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/validation"
"github.com/ignite/cli/v28/ignite/pkg/xstrings"
)
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/model/chain_debug_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmdmodel_test

import (
"errors"
"fmt"
"testing"

Expand All @@ -13,6 +12,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
cliuimodel "github.com/ignite/cli/v28/ignite/pkg/cliui/model"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/events"
)

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/model/chain_serve_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmdmodel_test

import (
"errors"
"fmt"
"strings"
"testing"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
cliuimodel "github.com/ignite/cli/v28/ignite/pkg/cliui/model"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/events"
)

Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/node_query.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ignitecmd

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/ignite/cli/v28/ignite/pkg/errors"
)

const (
Expand Down
20 changes: 10 additions & 10 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

Expand All @@ -16,6 +15,7 @@ import (
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v28/ignite/pkg/cosmosanalysis"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/gomodule"
"github.com/ignite/cli/v28/ignite/pkg/xgit"
"github.com/ignite/cli/v28/ignite/services/plugin"
Expand Down Expand Up @@ -77,7 +77,7 @@ func parseLocalPlugins(cmd *cobra.Command) (*pluginsconfig.Config, error) {
_ = cmd
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("parse local apps: %w", err)
return nil, errors.Errorf("parse local apps: %w", err)
}
if err := cosmosanalysis.IsChainPath(wd); err != nil {
return nil, err
Expand Down Expand Up @@ -215,7 +215,7 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
execHook := newExecutedHook(hook, cmd, args)
err = p.Interface.ExecuteHookPre(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
if err != nil {
return fmt.Errorf("app %q ExecuteHookPre() error: %w", p.Path, err)
return errors.Errorf("app %q ExecuteHookPre() error: %w", p.Path, err)
}
return nil
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)

err = p.Interface.ExecuteHookPost(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
if err != nil {
return fmt.Errorf("app %q ExecuteHookPost() error : %w", p.Path, err)
return errors.Errorf("app %q ExecuteHookPost() error : %w", p.Path, err)
}
return nil
}
Expand Down Expand Up @@ -480,7 +480,7 @@ Respects key value pairs declared after the app path to be added to the generate

for _, p := range conf.Apps {
if p.Path == args[0] {
return fmt.Errorf("app %s is already installed", args[0])
return errors.Errorf("app %s is already installed", args[0])
}
}

Expand All @@ -502,7 +502,7 @@ Respects key value pairs declared after the app path to be added to the generate
for _, pa := range pluginArgs {
kv := strings.Split(pa, "=")
if len(kv) != 2 {
return fmt.Errorf("malformed key=value arg: %s", pa)
return errors.Errorf("malformed key=value arg: %s", pa)
}
p.With[kv[0]] = kv[1]
}
Expand All @@ -515,7 +515,7 @@ Respects key value pairs declared after the app path to be added to the generate
defer plugins[0].KillClient()

if plugins[0].Error != nil {
return fmt.Errorf("error while loading app %q: %w", args[0], plugins[0].Error)
return errors.Errorf("error while loading app %q: %w", args[0], plugins[0].Error)
}
session.Println(icons.OK, "Done loading apps")
conf.Apps = append(conf.Apps, p)
Expand Down Expand Up @@ -571,7 +571,7 @@ func NewAppUninstall() *cobra.Command {

if !removed {
// return if no matching plugin path found
return fmt.Errorf("app %s not found", args[0])
return errors.Errorf("app %s not found", args[0])
}

if err := conf.Save(); err != nil {
Expand Down Expand Up @@ -651,7 +651,7 @@ func NewAppDescribe() *cobra.Command {
if p.Path == args[0] {
manifest, err := p.Interface.Manifest(ctx)
if err != nil {
return fmt.Errorf("error while loading app manifest: %w", err)
return errors.Errorf("error while loading app manifest: %w", err)
}

if len(manifest.Commands) > 0 {
Expand Down Expand Up @@ -705,7 +705,7 @@ func printPlugins(ctx context.Context, session *cliui.Session) error {
}

if err := session.PrintTable([]string{"Path", "Config", "Status"}, entries...); err != nil {
return fmt.Errorf("error while printing apps: %w", err)
return errors.Errorf("error while printing apps: %w", err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/relayer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ignitecmd

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/errors"
)

// NewRelayer returns a new relayer command.
Expand Down
Loading

0 comments on commit 1b08ba3

Please sign in to comment.