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 2 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
5 changes: 3 additions & 2 deletions client/rpc/apifile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -94,7 +95,7 @@ func (f *apiFile) ReadAt(p []byte, off int64) (int, error) {
defer resp.Output.Close()

n, err := io.ReadFull(resp.Output, p)
if err == io.ErrUnexpectedEOF {
if errors.Is(err, io.ErrUnexpectedEOF) {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
err = io.EOF
}
return n, err
Expand Down Expand Up @@ -170,7 +171,7 @@ func (it *apiIter) Next() bool {

var out lsOutput
if err := it.dec.Decode(&out); err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
it.err = err
}
return false
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -79,7 +80,7 @@
for {
var out struct{ Path string }
err := dec.Decode(&out)
if err == io.EOF {
if errors.Is(err, io.EOF) {

Check warning on line 83 in client/rpc/name.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/name.go#L83

Added line #L83 was not covered by tests
return
}
var ires iface.IpnsResult
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) {
}
}
if err := dec.Decode(&out); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
select {
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"

iface "github.com/ipfs/boxo/coreiface"
Expand Down Expand Up @@ -175,7 +176,7 @@ func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopt
for {
var msg pubsubMessage
if err := dec.Decode(&msg); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
msg.err = err
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, opts ...caopts.Unixfs
for {
var link lsOutput
if err := dec.Decode(&link); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
select {
Expand Down
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 @@
// 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)

Check warning on line 85 in cmd/ipfs/util/ulimit.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/util/ulimit.go#L85

Added line #L85 was not covered by tests
break
}
newLimit = targetLimit
Expand All @@ -107,7 +107,7 @@
break
}
default:
err = fmt.Errorf("error setting: ulimit: %s", err)
err = fmt.Errorf("error setting: ulimit: %w", err)

Check warning on line 110 in cmd/ipfs/util/ulimit.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/util/ulimit.go#L110

Added line #L110 was not covered by tests
}

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 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

Check warning on line 39 in config/bootstrap_peers.go

View check run for this annotation

Codecov / codecov/patch

config/bootstrap_peers.go#L39

Added line #L39 was not covered by tests
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 @@
}
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)

Check warning on line 120 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L120

Added line #L120 was not covered by tests
}
return &conf, nil
}
Expand All @@ -129,7 +129,7 @@
}
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)

Check warning on line 132 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L132

Added line #L132 was not covered by tests
}
return m, nil
}
Expand All @@ -140,11 +140,11 @@
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)

Check warning on line 143 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L143

Added line #L143 was not covered by tests
}

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)

Check warning on line 147 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L147

Added line #L147 was not covered by tests
}

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 @@
}
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)

Check warning on line 31 in config/serialize/serialize.go

View check run for this annotation

Codecov / codecov/patch

config/serialize/serialize.go#L31

Added line #L31 was not covered by tests
}
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 @@
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) {

Check warning on line 312 in core/bootstrap/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

core/bootstrap/bootstrap.go#L312

Added line #L312 was not covered by tests
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 @@

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)

Check warning on line 69 in repo/fsrepo/migrations/httpfetcher.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/httpfetcher.go#L69

Added line #L69 was not covered by tests
}

if f.userAgent != "" {
Expand All @@ -75,14 +75,14 @@

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)

Check warning on line 85 in repo/fsrepo/migrations/httpfetcher.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/httpfetcher.go#L85

Added line #L85 was not covered by tests
}
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 @@
}
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)

Check warning on line 35 in repo/fsrepo/migrations/migrations.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/migrations.go#L35

Added line #L35 was not covered by tests
}
if fromVer == targetVer {
// repo already at target version number
Expand Down Expand Up @@ -87,7 +87,7 @@
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 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)

Check warning on line 101 in repo/fsrepo/migrations/migrations.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/migrations.go#L101

Added line #L101 was not covered by tests
}

return vnum != target, nil
Expand Down Expand Up @@ -171,7 +171,7 @@
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 @@
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)

Check warning on line 296 in repo/fsrepo/migrations/migrations.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/migrations.go#L296

Added line #L296 was not covered by tests
}
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 @@
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())

Check warning on line 60 in repo/fsrepo/migrations/versions.go

View check run for this annotation

Codecov / codecov/patch

repo/fsrepo/migrations/versions.go#L60

Added line #L60 was not covered by tests
}

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 @@

import (
"context"
"errors"

"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -103,7 +104,7 @@
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 {

Check warning on line 107 in routing/composer.go

View check run for this annotation

Codecov / codecov/patch

routing/composer.go#L107

Added line #L107 was not covered by tests
out := make(chan []byte)
close(out)
return out, err
Expand Down
8 changes: 4 additions & 4 deletions tar/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ImportTar(ctx context.Context, r io.Reader, ds ipld.DAGService) (*dag.Proto
for {
h, err := tr.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand Down Expand Up @@ -128,7 +128,7 @@ func (tr *tarReader) Read(b []byte) (int, error) {
// no header remaining, check for recursive
if tr.childRead != nil {
n, err := tr.childRead.Read(b)
if err == io.EOF {
if errors.Is(err, io.EOF) {
tr.childRead = nil
return n, nil
}
Expand All @@ -138,7 +138,7 @@ func (tr *tarReader) Read(b []byte) (int, error) {
// check for filedata to be read
if tr.fileRead != nil {
n, err := tr.fileRead.Read(b)
if err == io.EOF {
if errors.Is(err, io.EOF) {
nr := tr.fileRead.n
tr.pad = (blockSize - (nr % blockSize)) % blockSize
tr.fileRead.Close()
Expand Down 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