Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: using error is instead of == [skip changelog] #10093

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/ipfs/util/ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
// set the soft value
err = setLimit(targetLimit, hard)
if err != nil {
err = fmt.Errorf("error setting ulimit without hard limit: %s", err)
err = fmt.Errorf("error setting ulimit without hard limit: %w", err)
break
}
newLimit = targetLimit
Expand All @@ -107,7 +107,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
break
}
default:
err = fmt.Errorf("error setting: ulimit: %s", err)
err = fmt.Errorf("error setting: ulimit: %w", err)
}

return newLimit > 0, newLimit, err
Expand Down
2 changes: 1 addition & 1 deletion config/bootstrap_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Config) BootstrapPeers() ([]peer.AddrInfo, error) {
func DefaultBootstrapPeers() ([]peer.AddrInfo, error) {
ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses)
if err != nil {
return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %s
return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %w
This is a problem with the ipfs codebase. Please report it to the dev team`, err)
}
return ps, nil
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func FromMap(v map[string]interface{}) (*Config, error) {
}
var conf Config
if err := json.NewDecoder(buf).Decode(&conf); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
return nil, fmt.Errorf("failure to decode config: %w", err)
}
return &conf, nil
}
Expand All @@ -129,7 +129,7 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
}
var m map[string]interface{}
if err := json.NewDecoder(buf).Decode(&m); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
return nil, fmt.Errorf("failure to decode config: %w", err)
}
return m, nil
}
Expand All @@ -140,11 +140,11 @@ func (c *Config) Clone() (*Config, error) {
var buf bytes.Buffer

if err := json.NewEncoder(&buf).Encode(c); err != nil {
return nil, fmt.Errorf("failure to encode config: %s", err)
return nil, fmt.Errorf("failure to encode config: %w", err)
}

if err := json.NewDecoder(&buf).Decode(&newConfig); err != nil {
return nil, fmt.Errorf("failure to decode config: %s", err)
return nil, fmt.Errorf("failure to decode config: %w", err)
}

return &newConfig, nil
Expand Down
2 changes: 1 addition & 1 deletion config/serialize/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
}
defer f.Close()
if err := json.NewDecoder(f).Decode(cfg); err != nil {
return fmt.Errorf("failure to decode config: %s", err)
return fmt.Errorf("failure to decode config: %w", err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrI
log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID)

if err := ph.Connect(ctx, p); err != nil {
if ctx.Err() != context.Canceled {
if !errors.Is(ctx.Err(), context.Canceled) {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
log.Debugf("failed to bootstrap with %v: %s", p.ID, err)
}
return
Expand Down
2 changes: 1 addition & 1 deletion repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (r *FSRepo) openConfig() error {
func (r *FSRepo) openUserResourceOverrides() error {
// This filepath is documented in docs/libp2p-resource-management.md and be kept in sync.
err := serialize.ReadConfigFile(filepath.Join(r.path, "libp2p-resource-limit-overrides.json"), &r.userResourceOverrides)
if err == serialize.ErrNotInitialized {
if errors.Is(err, serialize.ErrNotInitialized) {
err = nil
}
return err
Expand Down
6 changes: 3 additions & 3 deletions repo/fsrepo/migrations/httpfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error

req, err := http.NewRequestWithContext(ctx, http.MethodGet, gwURL, nil)
if err != nil {
return nil, fmt.Errorf("http.NewRequest error: %s", err)
return nil, fmt.Errorf("http.NewRequest error: %w", err)
}

if f.userAgent != "" {
Expand All @@ -75,14 +75,14 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("http.DefaultClient.Do error: %s", err)
return nil, fmt.Errorf("http.DefaultClient.Do error: %w", err)
}

if resp.StatusCode >= 400 {
defer resp.Body.Close()
mes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading error body: %s", err)
return nil, fmt.Errorf("error reading error body: %w", err)
}
return nil, fmt.Errorf("GET %s error: %s: %s", gwURL, resp.Status, string(mes))
}
Expand Down
10 changes: 5 additions & 5 deletions repo/fsrepo/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
}
fromVer, err := RepoVersion(ipfsDir)
if err != nil {
return fmt.Errorf("could not get repo version: %s", err)
return fmt.Errorf("could not get repo version: %w", err)
}
if fromVer == targetVer {
// repo already at target version number
Expand Down Expand Up @@ -87,7 +87,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
logger.Println("Running migration", migration, "...")
err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger)
if err != nil {
return fmt.Errorf("migration %s failed: %s", migration, err)
return fmt.Errorf("migration %s failed: %w", migration, err)
}
}
logger.Printf("Success: fs-repo migrated to version %d.\n", targetVer)
Expand All @@ -98,7 +98,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
func NeedMigration(target int) (bool, error) {
vnum, err := RepoVersion("")
if err != nil {
return false, fmt.Errorf("could not get repo version: %s", err)
return false, fmt.Errorf("could not get repo version: %w", err)
}

return vnum != target, nil
Expand Down Expand Up @@ -171,7 +171,7 @@ func GetMigrationFetcher(downloadSources []string, distPath string, newIpfsFetch
default:
u, err := url.Parse(src)
if err != nil {
return nil, fmt.Errorf("bad gateway address: %s", err)
return nil, fmt.Errorf("bad gateway address: %w", err)
}
switch u.Scheme {
case "":
Expand Down Expand Up @@ -293,7 +293,7 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest
if len(fails) != 0 {
err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " "))
if ctx.Err() != nil {
err = fmt.Errorf("%s, %s", ctx.Err(), err)
err = fmt.Errorf("%s, %w", ctx.Err(), err)
}
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion repo/fsrepo/migrations/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func DistVersions(ctx context.Context, fetcher Fetcher, dist string, sortDesc bo
vers = append(vers, ver)
}
if scan.Err() != nil {
return nil, fmt.Errorf("could not read versions: %s", scan.Err())
return nil, fmt.Errorf("could not read versions: %w", scan.Err())
}

if sortDesc {
Expand Down
3 changes: 2 additions & 1 deletion routing/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routing

import (
"context"
"errors"

"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (c *Composer) SearchValue(ctx context.Context, key string, opts ...routing.
ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...)

// avoid nil channels on implementations not supporting SearchValue method.
if err == routing.ErrNotFound && ch == nil {
if errors.Is(err, routing.ErrNotFound) && ch == nil {
out := make(chan []byte)
close(out)
return out, err
Expand Down
2 changes: 1 addition & 1 deletion tar/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (tr *tarReader) Read(b []byte) (int, error) {
tr.hdrBuf = bytes.NewReader(hndpb.Data())

dataNd, err := hndpb.GetLinkedProtoNode(tr.ctx, tr.ds, "data")
if err != nil && err != dag.ErrLinkNotFound {
if err != nil && !errors.Is(err, dag.ErrLinkNotFound) {
return 0, err
}

Expand Down
Loading