refactor: using error is instead of == (#10093)
Kay committed
Aug 22, 2023 at 18:23 UTC
2b7c20fc66faeea5c74c8d0c14691238f2304553
10 files changed
+21
-20
cmd/ipfs/util/ulimit.go
+2
-2
@@ -82,7 +82,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
82
// set the soft value
83
err = setLimit(targetLimit, hard)
84
if err != nil {
85
- err = fmt.Errorf("error setting ulimit without hard limit: %s", err)
85
+ err = fmt.Errorf("error setting ulimit without hard limit: %w", err)
86
break
87
}
88
newLimit = targetLimit
@@ -107,7 +107,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
107
break
108
}
109
default:
110
- err = fmt.Errorf("error setting: ulimit: %s", err)
110
+ err = fmt.Errorf("error setting: ulimit: %w", err)
111
}
112
113
return newLimit > 0, newLimit, err
config/bootstrap_peers.go
+1
-1
@@ -36,7 +36,7 @@ func (c *Config) BootstrapPeers() ([]peer.AddrInfo, error) {
36
func DefaultBootstrapPeers() ([]peer.AddrInfo, error) {
37
ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses)
38
if err != nil {
39
- return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %s
39
+ return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %w
40
This is a problem with the ipfs codebase. Please report it to the dev team`, err)
41
}
42
return ps, nil
config/config.go
+4
-4
@@ -117,7 +117,7 @@ func FromMap(v map[string]interface{}) (*Config, error) {
117
}
118
var conf Config
119
if err := json.NewDecoder(buf).Decode(&conf); err != nil {
120
- return nil, fmt.Errorf("failure to decode config: %s", err)
120
+ return nil, fmt.Errorf("failure to decode config: %w", err)
121
}
122
return &conf, nil
123
}
@@ -129,7 +129,7 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
129
}
130
var m map[string]interface{}
131
if err := json.NewDecoder(buf).Decode(&m); err != nil {
132
- return nil, fmt.Errorf("failure to decode config: %s", err)
132
+ return nil, fmt.Errorf("failure to decode config: %w", err)
133
}
134
return m, nil
135
}
@@ -140,11 +140,11 @@ func (c *Config) Clone() (*Config, error) {
140
var buf bytes.Buffer
141
142
if err := json.NewEncoder(&buf).Encode(c); err != nil {
143
- return nil, fmt.Errorf("failure to encode config: %s", err)
143
+ return nil, fmt.Errorf("failure to encode config: %w", err)
144
}
145
146
if err := json.NewDecoder(&buf).Decode(&newConfig); err != nil {
147
- return nil, fmt.Errorf("failure to decode config: %s", err)
147
+ return nil, fmt.Errorf("failure to decode config: %w", err)
148
}
149
150
return &newConfig, nil
config/serialize/serialize.go
+1
-1
@@ -28,7 +28,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
28
}
29
defer f.Close()
30
if err := json.NewDecoder(f).Decode(cfg); err != nil {
31
- return fmt.Errorf("failure to decode config: %s", err)
31
+ return fmt.Errorf("failure to decode config: %w", err)
32
}
33
return nil
34
}
repo/fsrepo/fsrepo.go
+1
-1
@@ -449,7 +449,7 @@ func (r *FSRepo) openConfig() error {
449
func (r *FSRepo) openUserResourceOverrides() error {
450
// This filepath is documented in docs/libp2p-resource-management.md and be kept in sync.
451
err := serialize.ReadConfigFile(filepath.Join(r.path, "libp2p-resource-limit-overrides.json"), &r.userResourceOverrides)
452
- if err == serialize.ErrNotInitialized {
452
+ if errors.Is(err, serialize.ErrNotInitialized) {
453
err = nil
454
}
455
return err
repo/fsrepo/migrations/httpfetcher.go
+3
-3
@@ -66,7 +66,7 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error
66
67
req, err := http.NewRequestWithContext(ctx, http.MethodGet, gwURL, nil)
68
if err != nil {
69
- return nil, fmt.Errorf("http.NewRequest error: %s", err)
69
+ return nil, fmt.Errorf("http.NewRequest error: %w", err)
70
}
71
72
if f.userAgent != "" {
@@ -75,14 +75,14 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error
75
76
resp, err := http.DefaultClient.Do(req)
77
if err != nil {
78
- return nil, fmt.Errorf("http.DefaultClient.Do error: %s", err)
78
+ return nil, fmt.Errorf("http.DefaultClient.Do error: %w", err)
79
}
80
81
if resp.StatusCode >= 400 {
82
defer resp.Body.Close()
83
mes, err := io.ReadAll(resp.Body)
84
if err != nil {
85
- return nil, fmt.Errorf("error reading error body: %s", err)
85
+ return nil, fmt.Errorf("error reading error body: %w", err)
86
}
87
return nil, fmt.Errorf("GET %s error: %s: %s", gwURL, resp.Status, string(mes))
88
}
repo/fsrepo/migrations/migrations.go
+5
-5
@@ -32,7 +32,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
32
}
33
fromVer, err := RepoVersion(ipfsDir)
34
if err != nil {
35
- return fmt.Errorf("could not get repo version: %s", err)
35
+ return fmt.Errorf("could not get repo version: %w", err)
36
}
37
if fromVer == targetVer {
38
// repo already at target version number
@@ -87,7 +87,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
87
logger.Println("Running migration", migration, "...")
88
err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger)
89
if err != nil {
90
- return fmt.Errorf("migration %s failed: %s", migration, err)
90
+ return fmt.Errorf("migration %s failed: %w", migration, err)
91
}
92
}
93
logger.Printf("Success: fs-repo migrated to version %d.\n", targetVer)
@@ -98,7 +98,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
98
func NeedMigration(target int) (bool, error) {
99
vnum, err := RepoVersion("")
100
if err != nil {
101
- return false, fmt.Errorf("could not get repo version: %s", err)
101
+ return false, fmt.Errorf("could not get repo version: %w", err)
102
}
103
104
return vnum != target, nil
@@ -171,7 +171,7 @@ func GetMigrationFetcher(downloadSources []string, distPath string, newIpfsFetch
171
default:
172
u, err := url.Parse(src)
173
if err != nil {
174
- return nil, fmt.Errorf("bad gateway address: %s", err)
174
+ return nil, fmt.Errorf("bad gateway address: %w", err)
175
}
176
switch u.Scheme {
177
case "":
@@ -293,7 +293,7 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest
293
if len(fails) != 0 {
294
err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " "))
295
if ctx.Err() != nil {
296
- err = fmt.Errorf("%s, %s", ctx.Err(), err)
296
+ err = fmt.Errorf("%s, %w", ctx.Err(), err)
297
}
298
return nil, err
299
}
repo/fsrepo/migrations/versions.go
+1
-1
@@ -57,7 +57,7 @@ func DistVersions(ctx context.Context, fetcher Fetcher, dist string, sortDesc bo
57
vers = append(vers, ver)
58
}
59
if scan.Err() != nil {
60
- return nil, fmt.Errorf("could not read versions: %s", scan.Err())
60
+ return nil, fmt.Errorf("could not read versions: %w", scan.Err())
61
}
62
63
if sortDesc {
routing/composer.go
+2
-1
@@ -2,6 +2,7 @@ package routing
2
3
import (
4
"context"
5
+ "errors"
6
7
"github.com/hashicorp/go-multierror"
8
"github.com/ipfs/go-cid"
@@ -103,7 +104,7 @@ func (c *Composer) SearchValue(ctx context.Context, key string, opts ...routing.
104
ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...)
105
106
// avoid nil channels on implementations not supporting SearchValue method.
106
- if err == routing.ErrNotFound && ch == nil {
107
+ if errors.Is(err, routing.ErrNotFound) && ch == nil {
108
out := make(chan []byte)
109
close(out)
110
return out, err
tar/format.go
+1
-1
@@ -175,7 +175,7 @@ func (tr *tarReader) Read(b []byte) (int, error) {
175
tr.hdrBuf = bytes.NewReader(hndpb.Data())
176
177
dataNd, err := hndpb.GetLinkedProtoNode(tr.ctx, tr.ds, "data")
178
- if err != nil && err != dag.ErrLinkNotFound {
178
+ if err != nil && !errors.Is(err, dag.ErrLinkNotFound) {
179
return 0, err
180
}
181