Skip to content

use standard errors instead of github.com/pkg/errors #3203

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ run:
linters:
disable-all: true
enable:
- misspell
- errorlint
- gofmt
- goimports
- govet
- ineffassign
- misspell
- revive
- unconvert
- unused
- govet

7 changes: 4 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package agent
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"reflect"
"sync"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -467,7 +468,7 @@ func (a *Agent) handleSessionMessage(ctx context.Context, message *api.SessionMe
if !same {
a.keys = message.NetworkBootstrapKeys
if err := a.config.Executor.SetNetworkBootstrapKeys(a.keys); err != nil {
return errors.Wrap(err, "configuring network key failed")
return fmt.Errorf("configuring network key failed: %w", err)
}
}
}
Expand Down Expand Up @@ -517,7 +518,7 @@ func (a *Agent) UpdateTaskStatus(ctx context.Context, taskID string, status *api
go func() {
err := session.sendTaskStatus(ctx, taskID, status)
if err != nil {
if err == errTaskUnknown {
if errors.Is(err, errTaskUnknown) {
err = nil // dispatcher no longer cares about this task.
} else {
log.G(ctx).WithError(err).Error("closing session after fatal error")
Expand Down
3 changes: 2 additions & 1 deletion agent/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package agent

import (
"errors"

"github.com/docker/go-events"
"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/connectionbroker"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
"google.golang.org/grpc/credentials"
)
Expand Down
5 changes: 3 additions & 2 deletions agent/csi/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugin

import (
"context"
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -57,7 +58,7 @@ func (pm *pluginManager) Get(name string) (NodePlugin, error) {

plugin, err := pm.getPlugin(name)
if err != nil {
return nil, fmt.Errorf("cannot get plugin %v: %v", name, err)
return nil, fmt.Errorf("cannot get plugin %v: %w", name, err)
}

return plugin, nil
Expand Down Expand Up @@ -110,7 +111,7 @@ func (pm *pluginManager) getPlugin(name string) (NodePlugin, error) {

pa, ok := pc.(plugin.AddrPlugin)
if !ok {
return nil, fmt.Errorf("plugin does not implement PluginAddr interface")
return nil, errors.New("plugin does not implement PluginAddr interface")
}

p := pm.newNodePluginFunc(name, pa, pm.secrets)
Expand Down
3 changes: 2 additions & 1 deletion agent/csi/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package csi

import (
"context"
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (r *volumes) Get(volumeID string) (string, error) {
if vs, ok := r.volumes[volumeID]; ok {
if vs.remove {
// TODO(dperny): use a structured error
return "", fmt.Errorf("volume being removed")
return "", errors.New("volume being removed")
}

if p, err := r.plugins.Get(vs.volume.Driver.Name); err == nil {
Expand Down
11 changes: 5 additions & 6 deletions agent/exec/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package exec

import (
"context"
"errors"
"fmt"
"time"

"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/api/equality"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/protobuf/ptypes"
"github.com/pkg/errors"
)

// Controller controls execution of a task.
Expand Down Expand Up @@ -197,7 +197,7 @@ func Do(ctx context.Context, task *api.Task, ctlr Controller) (*api.TaskStatus,
exitCode = ec.ExitCode()
}

if cause := errors.Cause(err); cause == context.DeadlineExceeded || cause == context.Canceled {
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
return retry()
}

Expand Down Expand Up @@ -308,13 +308,13 @@ func Do(ctx context.Context, task *api.Task, ctlr Controller) (*api.TaskStatus,
// the following states may proceed past desired state.
switch status.State {
case api.TaskStatePreparing:
if err := ctlr.Prepare(ctx); err != nil && err != ErrTaskPrepared {
if err := ctlr.Prepare(ctx); err != nil && !errors.Is(err, ErrTaskPrepared) {
return fatal(err)
}

return transition(api.TaskStateReady, "prepared")
case api.TaskStateStarting:
if err := ctlr.Start(ctx); err != nil && err != ErrTaskStarted {
if err := ctlr.Start(ctx); err != nil && !errors.Is(err, ErrTaskStarted) {
return fatal(err)
}

Expand Down Expand Up @@ -355,6 +355,5 @@ func logStateChange(ctx context.Context, desired, previous, next api.TaskState)
}

func contextDoneError(err error) bool {
cause := errors.Cause(err)
return cause == context.Canceled || cause == context.DeadlineExceeded
return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
}
2 changes: 1 addition & 1 deletion agent/exec/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package exec

import "github.com/pkg/errors"
import "errors"

var (
// ErrRuntimeUnsupported encountered when a task requires a runtime
Expand Down
6 changes: 2 additions & 4 deletions agent/exec/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package exec
import (
"fmt"
"testing"

"github.com/pkg/errors"
)

func TestIsTemporary(t *testing.T) {
err := fmt.Errorf("err")
err1 := MakeTemporary(fmt.Errorf("err1: %w", err))
err2 := fmt.Errorf("err2: %w", err1)
err3 := errors.Wrap(err2, "err3")
err3 := fmt.Errorf("err3: %w", err2)
err4 := fmt.Errorf("err4: %w", err3)
err5 := errors.Wrap(err4, "err5")
err5 := fmt.Errorf("err5: %w", err4)

if IsTemporary(nil) {
t.Error("expected error to not be a temporary error")
Expand Down
9 changes: 5 additions & 4 deletions agent/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"sync"
"time"

Expand Down Expand Up @@ -160,20 +161,20 @@ func (tm *taskManager) run(ctx context.Context) {
default:
}

switch err {
case exec.ErrTaskNoop:
switch {
case errors.Is(err, exec.ErrTaskNoop):
if !updated {
continue // wait till getting pumped via update.
}
case exec.ErrTaskRetry:
case errors.Is(err, exec.ErrTaskRetry):
// TODO(stevvooe): Add exponential backoff with random jitter
// here. For now, this backoff is enough to keep the task
// manager from running away with the CPU.
time.AfterFunc(time.Second, func() {
errs <- nil // repump this branch, with no err
})
continue
case nil, context.Canceled, context.DeadlineExceeded:
case err == nil, errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
// no log in this case
default:
log.G(ctx).WithError(err).Error("task operation failed")
Expand Down
7 changes: 4 additions & 3 deletions agent/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"context"
"errors"
"sync"

"github.com/moby/swarmkit/v2/agent/exec"
Expand Down Expand Up @@ -239,15 +240,15 @@ func reconcileTaskState(ctx context.Context, w *worker, assignments []*api.Assig
}

if mgr, ok := w.taskManagers[task.ID]; ok {
if err := mgr.Update(ctx, task); err != nil && err != ErrClosed {
if err := mgr.Update(ctx, task); err != nil && !errors.Is(err, ErrClosed) {
log.G(ctx).WithError(err).Error("failed updating assigned task")
}
} else {
// we may have still seen the task, let's grab the status from
// storage and replace it with our status, if we have it.
status, err := GetTaskStatus(tx, task.ID)
if err != nil {
if err != errTaskUnknown {
if !errors.Is(err, errTaskUnknown) {
return err
}

Expand Down Expand Up @@ -569,7 +570,7 @@ func (w *worker) updateTaskStatus(ctx context.Context, tx *bolt.Tx, taskID strin
// dance of too-tightly-coupled concurrent parts, fixing tht race is
// fraught with hazards. instead, we'll recognize that it can occur,
// log the error, and then ignore it.
if err == errTaskUnknown {
if errors.Is(err, errTaskUnknown) {
// log at info level. debug logging in docker is already really
// verbose, so many people disable it. the race that causes this
// behavior should be very rare, but if it occurs, we should know
Expand Down
5 changes: 3 additions & 2 deletions api/genericresource/resource_management.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genericresource

import (
"errors"
"fmt"

"github.com/moby/swarmkit/v2/api"
Expand All @@ -15,7 +16,7 @@ func Claim(nodeAvailableResources, taskAssigned *[]*api.GenericResource,
for _, res := range taskReservations {
tr := res.GetDiscreteResourceSpec()
if tr == nil {
return fmt.Errorf("task should only hold Discrete type")
return errors.New("task should only hold Discrete type")
}

// Select the resources
Expand Down Expand Up @@ -86,7 +87,7 @@ func Reclaim(nodeAvailableResources *[]*api.GenericResource, taskAssigned, nodeR
func reclaimResources(nodeAvailableResources *[]*api.GenericResource, taskAssigned []*api.GenericResource) error {
// The node could have been updated
if nodeAvailableResources == nil {
return fmt.Errorf("node no longer has any resources")
return errors.New("node no longer has any resources")
}

for _, res := range taskAssigned {
Expand Down
3 changes: 2 additions & 1 deletion api/genericresource/validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genericresource

import (
"errors"
"fmt"

"github.com/moby/swarmkit/v2/api"
Expand All @@ -24,7 +25,7 @@ func ValidateTask(resources *api.Resources) error {
func HasEnough(nodeRes []*api.GenericResource, taskRes *api.GenericResource) (bool, error) {
t := taskRes.GetDiscreteResourceSpec()
if t == nil {
return false, fmt.Errorf("task should only hold Discrete type")
return false, errors.New("task should only hold Discrete type")
}

if nodeRes == nil {
Expand Down
2 changes: 1 addition & 1 deletion api/storeobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func customIndexer(kind string, annotations *Annotations) (bool, [][]byte, error

func fromArgs(args ...interface{}) ([]byte, error) {
if len(args) != 1 {
return nil, fmt.Errorf("must provide only a single argument")
return nil, errors.New("must provide only a single argument")
}
arg, ok := args[0].(string)
if !ok {
Expand Down
Loading