mirror of
https://github.com/ipfs/kubo.git
synced 2025-05-17 15:06:47 +08:00
refactor: using error is instead of == (#10093)
This commit is contained in:
@ -82,7 +82,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
|
|||||||
// set the soft value
|
// set the soft value
|
||||||
err = setLimit(targetLimit, hard)
|
err = setLimit(targetLimit, hard)
|
||||||
if err != nil {
|
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
|
break
|
||||||
}
|
}
|
||||||
newLimit = targetLimit
|
newLimit = targetLimit
|
||||||
@ -107,7 +107,7 @@ func ManageFdLimit() (changed bool, newLimit uint64, err error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("error setting: ulimit: %s", err)
|
err = fmt.Errorf("error setting: ulimit: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newLimit > 0, newLimit, err
|
return newLimit > 0, newLimit, err
|
||||||
|
@ -36,7 +36,7 @@ func (c *Config) BootstrapPeers() ([]peer.AddrInfo, error) {
|
|||||||
func DefaultBootstrapPeers() ([]peer.AddrInfo, error) {
|
func DefaultBootstrapPeers() ([]peer.AddrInfo, error) {
|
||||||
ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses)
|
ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses)
|
||||||
if err != nil {
|
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)
|
This is a problem with the ipfs codebase. Please report it to the dev team`, err)
|
||||||
}
|
}
|
||||||
return ps, nil
|
return ps, nil
|
||||||
|
@ -117,7 +117,7 @@ func FromMap(v map[string]interface{}) (*Config, error) {
|
|||||||
}
|
}
|
||||||
var conf Config
|
var conf Config
|
||||||
if err := json.NewDecoder(buf).Decode(&conf); err != nil {
|
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
|
return &conf, nil
|
||||||
}
|
}
|
||||||
@ -129,7 +129,7 @@ func ToMap(conf *Config) (map[string]interface{}, error) {
|
|||||||
}
|
}
|
||||||
var m map[string]interface{}
|
var m map[string]interface{}
|
||||||
if err := json.NewDecoder(buf).Decode(&m); err != nil {
|
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
|
return m, nil
|
||||||
}
|
}
|
||||||
@ -140,11 +140,11 @@ func (c *Config) Clone() (*Config, error) {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
if err := json.NewEncoder(&buf).Encode(c); err != nil {
|
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 {
|
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
|
return &newConfig, nil
|
||||||
|
@ -28,7 +28,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if err := json.NewDecoder(f).Decode(cfg); err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -449,7 +449,7 @@ func (r *FSRepo) openConfig() error {
|
|||||||
func (r *FSRepo) openUserResourceOverrides() error {
|
func (r *FSRepo) openUserResourceOverrides() error {
|
||||||
// This filepath is documented in docs/libp2p-resource-management.md and be kept in sync.
|
// 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)
|
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
|
err = nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
@ -66,7 +66,7 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error
|
|||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, gwURL, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, gwURL, nil)
|
||||||
if err != 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 != "" {
|
if f.userAgent != "" {
|
||||||
@ -75,14 +75,14 @@ func (f *HttpFetcher) Fetch(ctx context.Context, filePath string) ([]byte, error
|
|||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
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 {
|
if resp.StatusCode >= 400 {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
mes, err := io.ReadAll(resp.Body)
|
mes, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
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))
|
return nil, fmt.Errorf("GET %s error: %s: %s", gwURL, resp.Status, string(mes))
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
|
|||||||
}
|
}
|
||||||
fromVer, err := RepoVersion(ipfsDir)
|
fromVer, err := RepoVersion(ipfsDir)
|
||||||
if err != nil {
|
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 {
|
if fromVer == targetVer {
|
||||||
// repo already at target version number
|
// repo already at target version number
|
||||||
@ -87,7 +87,7 @@ func RunMigration(ctx context.Context, fetcher Fetcher, targetVer int, ipfsDir s
|
|||||||
logger.Println("Running migration", migration, "...")
|
logger.Println("Running migration", migration, "...")
|
||||||
err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger)
|
err = runMigration(ctx, binPaths[migration], ipfsDir, revert, logger)
|
||||||
if err != nil {
|
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)
|
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
|
|||||||
func NeedMigration(target int) (bool, error) {
|
func NeedMigration(target int) (bool, error) {
|
||||||
vnum, err := RepoVersion("")
|
vnum, err := RepoVersion("")
|
||||||
if err != nil {
|
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
|
return vnum != target, nil
|
||||||
@ -171,7 +171,7 @@ func GetMigrationFetcher(downloadSources []string, distPath string, newIpfsFetch
|
|||||||
default:
|
default:
|
||||||
u, err := url.Parse(src)
|
u, err := url.Parse(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("bad gateway address: %s", err)
|
return nil, fmt.Errorf("bad gateway address: %w", err)
|
||||||
}
|
}
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "":
|
case "":
|
||||||
@ -293,7 +293,7 @@ func fetchMigrations(ctx context.Context, fetcher Fetcher, needed []string, dest
|
|||||||
if len(fails) != 0 {
|
if len(fails) != 0 {
|
||||||
err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " "))
|
err = fmt.Errorf("failed to download migrations: %s", strings.Join(fails, " "))
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
err = fmt.Errorf("%s, %s", ctx.Err(), err)
|
err = fmt.Errorf("%s, %w", ctx.Err(), err)
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ func DistVersions(ctx context.Context, fetcher Fetcher, dist string, sortDesc bo
|
|||||||
vers = append(vers, ver)
|
vers = append(vers, ver)
|
||||||
}
|
}
|
||||||
if scan.Err() != nil {
|
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 {
|
if sortDesc {
|
||||||
|
@ -2,6 +2,7 @@ package routing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
@ -103,7 +104,7 @@ func (c *Composer) SearchValue(ctx context.Context, key string, opts ...routing.
|
|||||||
ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...)
|
ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...)
|
||||||
|
|
||||||
// avoid nil channels on implementations not supporting SearchValue method.
|
// 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)
|
out := make(chan []byte)
|
||||||
close(out)
|
close(out)
|
||||||
return out, err
|
return out, err
|
||||||
|
@ -175,7 +175,7 @@ func (tr *tarReader) Read(b []byte) (int, error) {
|
|||||||
tr.hdrBuf = bytes.NewReader(hndpb.Data())
|
tr.hdrBuf = bytes.NewReader(hndpb.Data())
|
||||||
|
|
||||||
dataNd, err := hndpb.GetLinkedProtoNode(tr.ctx, tr.ds, "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
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user