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

Switch from fmt.Errorf to xerrors.Errorf #5425

Merged
merged 3 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions components/blobserve/pkg/blobserve/refstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/google/go-cmp/cmp"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

func TestBlobFor(t *testing.T) {
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestBlobFor(t *testing.T) {
ExtraAction: func(t *testing.T, s *refstore) (err error) {
_, _, err = s.BlobFor(context.Background(), refDescriptor, false)
if err == nil {
return fmt.Errorf("found layer although we shouldn't have")
return xerrors.Errorf("found layer although we shouldn't have")
}
s.Resolver = func() remotes.Resolver {
return &fakeFetcher{
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestBlobFor(t *testing.T) {
}
_, _, err := s.BlobFor(ctx, refDescriptor, false)
if err != nil {
return fmt.Errorf("client %03d: %w", i, err)
return xerrors.Errorf("client %03d: %w", i, err)
}
return nil
})
Expand Down Expand Up @@ -355,11 +355,11 @@ func TestBlobFor(t *testing.T) {
oadd := bs.Adder

bs.Adder = func(ctx context.Context, name string, in io.Reader) (err error) {
return fmt.Errorf("failed to download")
return xerrors.Errorf("failed to download")
}
_, _, err := s.BlobFor(context.Background(), refDescriptor, false)
if err == nil {
return fmt.Errorf("first download didn't fail")
return xerrors.Errorf("first download didn't fail")
}

bs.Adder = oadd
Expand Down Expand Up @@ -457,7 +457,7 @@ func (f *fakeFetcher) Resolve(ctx context.Context, ref string) (name string, des
name = ref
fc, ok := f.Content[ref]
if !ok {
err = fmt.Errorf("not found")
err = xerrors.Errorf("not found")
return
}
c, err := fc()
Expand All @@ -471,7 +471,7 @@ func (f *fakeFetcher) Resolve(ctx context.Context, ref string) (name string, des

// Pusher returns a new pusher for the provided reference
func (f *fakeFetcher) Pusher(ctx context.Context, ref string) (remotes.Pusher, error) {
return nil, fmt.Errorf("not implemented")
return nil, xerrors.Errorf("not implemented")
}

// Fetcher returns a new fetcher for the provided reference.
Expand All @@ -484,7 +484,7 @@ func (f *fakeFetcher) Fetcher(ctx context.Context, ref string) (remotes.Fetcher,
func (f *fakeFetcher) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadCloser, error) {
fc, ok := f.Content[desc.Digest.Encoded()]
if !ok {
return nil, fmt.Errorf("%s not found", desc.Digest.Encoded())
return nil, xerrors.Errorf("%s not found", desc.Digest.Encoded())
}
c, err := fc()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type fromBackupInitializer struct {
func (bi *fromBackupInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (src csapi.WorkspaceInitSource, err error) {
hasBackup, err := bi.RemoteStorage.Download(ctx, bi.Location, storage.DefaultBackup, mappings)
if !hasBackup {
return src, fmt.Errorf("no backup found")
return src, xerrors.Errorf("no backup found")
}
if err != nil {
return src, xerrors.Errorf("cannot restore backup: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion components/content-service/pkg/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"path/filepath"
"strings"

"golang.org/x/xerrors"
)

const (
Expand Down Expand Up @@ -84,7 +86,7 @@ func ParseTaskIDFromPrebuildLogFilePath(filePath string) (string, error) {
streamID = strings.TrimPrefix(fileName, prebuildLogFilePrefix)
}
if streamID == "" {
return "", fmt.Errorf("cannot parse stream ID from filePath: '%s'", fileName)
return "", xerrors.Errorf("cannot parse stream ID from filePath: '%s'", fileName)
}

return streamID, nil
Expand Down
2 changes: 1 addition & 1 deletion components/content-service/pkg/storage/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (rs *DirectGCPStorage) Qualify(name string) string {
// UploadInstance takes all files from a local location and uploads it to the per-instance remote storage
func (rs *DirectGCPStorage) UploadInstance(ctx context.Context, source string, name string, opts ...UploadOption) (bucket, object string, err error) {
if rs.InstanceID == "" {
return "", "", fmt.Errorf("instanceID is required to comput object name")
return "", "", xerrors.Errorf("instanceID is required to comput object name")
}
return rs.Upload(ctx, source, InstanceObjectName(rs.InstanceID, name), opts...)
}
Expand Down
2 changes: 1 addition & 1 deletion components/content-service/pkg/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (rs *DirectMinIOStorage) Qualify(name string) string {
// UploadInstance takes all files from a local location and uploads it to the per-instance remote storage
func (rs *DirectMinIOStorage) UploadInstance(ctx context.Context, source string, name string, opts ...UploadOption) (bucket, object string, err error) {
if rs.InstanceID == "" {
return "", "", fmt.Errorf("instanceID is required to comput object name")
return "", "", xerrors.Errorf("instanceID is required to comput object name")
}
return rs.Upload(ctx, source, InstanceObjectName(rs.InstanceID, name), opts...)
}
Expand Down
2 changes: 1 addition & 1 deletion components/content-service/pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

var (
// ErrNotFound is returned when an object is not found
ErrNotFound = fmt.Errorf("not found")
ErrNotFound = xerrors.Errorf("not found")
)

// BucketNamer provides names for storage buckets
Expand Down
11 changes: 6 additions & 5 deletions components/docker-up/docker-up/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"golang.org/x/sys/unix"
"golang.org/x/xerrors"
)

var log *logrus.Entry
Expand Down Expand Up @@ -106,7 +107,7 @@ func runWithinNetns() (err error) {
return err
}
if msg.Stage != 1 {
return fmt.Errorf("expected stage 1 message, got %+q", msg)
return xerrors.Errorf("expected stage 1 message, got %+q", msg)
}
log.Debug("parent is ready")

Expand Down Expand Up @@ -284,7 +285,7 @@ func ensurePrerequisites() error {
return nil
}

errMissingPrerequisites := fmt.Errorf("missing prerequisites")
errMissingPrerequisites := xerrors.Errorf("missing prerequisites")
if !opts.AutoInstall {
return errMissingPrerequisites
}
Expand Down Expand Up @@ -323,7 +324,7 @@ func installDocker() error {
return nil
}

return fmt.Errorf("Unable to extract container: %v\n", err)
return xerrors.Errorf("Unable to extract container: %v\n", err)
}

hdrInfo := hdr.FileInfo()
Expand All @@ -340,12 +341,12 @@ func installDocker() error {
case tar.TypeReg, tar.TypeRegA:
file, err := os.OpenFile(dstpath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
if err != nil {
return fmt.Errorf("unable to create file: %v", err)
return xerrors.Errorf("unable to create file: %v", err)
}

if _, err := io.Copy(file, tarReader); err != nil {
file.Close()
return fmt.Errorf("unable to write file: %v", err)
return xerrors.Errorf("unable to write file: %v", err)
}

file.Close()
Expand Down
12 changes: 6 additions & 6 deletions components/docker-up/runc-facade/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package main

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"syscall"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"golang.org/x/xerrors"
)

var (
Expand Down Expand Up @@ -50,31 +50,31 @@ func main() {
func createAndRunc(runcPath string) error {
fc, err := os.ReadFile("config.json")
if err != nil {
return fmt.Errorf("cannot read config.json: %w", err)
return xerrors.Errorf("cannot read config.json: %w", err)
}

var cfg specs.Spec
err = json.Unmarshal(fc, &cfg)
if err != nil {
return fmt.Errorf("cannot decode config.json: %w", err)
return xerrors.Errorf("cannot decode config.json: %w", err)
}

cfg.Process.OOMScoreAdj = &defaultOOMScoreAdj

fc, err = json.Marshal(cfg)
if err != nil {
return fmt.Errorf("cannot encode config.json: %w", err)
return xerrors.Errorf("cannot encode config.json: %w", err)
}
for _, fn := range []string{"config.json", "/tmp/debug.json"} {
err = os.WriteFile(fn, fc, 0644)
if err != nil {
return fmt.Errorf("cannot encode config.json: %w", err)
return xerrors.Errorf("cannot encode config.json: %w", err)
}
}

err = syscall.Exec(runcPath, os.Args, os.Environ())
if err != nil {
return fmt.Errorf("exec %s: %w", runcPath, err)
return xerrors.Errorf("exec %s: %w", runcPath, err)
}
return nil
}
4 changes: 2 additions & 2 deletions components/docker-up/slirp-docker-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func xmain(f *os.File) error {
if examplePort == 0 {
examplePort = 8080
}
return fmt.Errorf("Workspace (host) port needs to be > 1024, e.g. %d:%d instead of %d:%d", examplePort, *containerPort, *hostPort, *containerPort)
return xerrors.Errorf("Workspace (host) port needs to be > 1024, e.g. %d:%d instead of %d:%d", examplePort, *containerPort, *hostPort, *containerPort)
}

id, err := exposePort(socketPath)
Expand Down Expand Up @@ -196,7 +196,7 @@ func sendRequest(socketPath string, req request) (resp map[string]interface{}, e
}

if len(rep.Error) > 0 {
return nil, fmt.Errorf("error reply: %+v", rep.Error)
return nil, xerrors.Errorf("error reply: %+v", rep.Error)
}
return rep.Return, nil
}
3 changes: 2 additions & 1 deletion components/ee/agent-smith/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

// runCmd represents the run command
Expand Down Expand Up @@ -182,7 +183,7 @@ func notifySlack(webhook string, hostURL string, ws agent.InfringingWorkspace, p
for i, err := range errs {
allerr[i] = err.Error()
}
return fmt.Errorf("notifySlack: %s", strings.Join(allerr, ", "))
return xerrors.Errorf("notifySlack: %s", strings.Join(allerr, ", "))
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions components/ee/agent-smith/pkg/agent/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package agent

import (
"context"
"fmt"

wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
"github.com/gitpod-io/gitpod/common-go/log"
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"

"golang.org/x/sys/unix"
"golang.org/x/xerrors"
corev1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
)
Expand All @@ -38,7 +38,7 @@ func (agent *Smith) stopWorkspaceAndBlockUser(supervisorPID int, ownerID string)

func (agent *Smith) blockUser(ownerID string) error {
if agent.GitpodAPI == nil {
return fmt.Errorf("not connected to Gitpod API")
return xerrors.Errorf("not connected to Gitpod API")
}

req := protocol.AdminBlockUserRequest{
Expand All @@ -50,10 +50,10 @@ func (agent *Smith) blockUser(ownerID string) error {

func (agent *Smith) limitCPUUse(podname string) error {
if agent.Kubernetes == nil {
return fmt.Errorf("not connected to Kubernetes - cannot limit CPU usage")
return xerrors.Errorf("not connected to Kubernetes - cannot limit CPU usage")
}
if agent.Config.Enforcement.CPULimitPenalty == "" {
return fmt.Errorf("no CPU limit penalty specified - cannot limit CPU usage")
return xerrors.Errorf("no CPU limit penalty specified - cannot limit CPU usage")
}

ctx := context.Background()
Expand Down
8 changes: 4 additions & 4 deletions components/ee/agent-smith/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (g GradedInfringementKind) Kind() (InfringementKind, error) {
}
}

return "", fmt.Errorf("unknown kind")
return "", xerrors.Errorf("unknown kind")
}

// defaultRuleset is the name ("remote origin URL") of the default enforcement rules
Expand All @@ -334,7 +334,7 @@ type EnforcementRules map[GradedInfringementKind]PenaltyKind
func (er EnforcementRules) Validate() error {
for k := range er {
if _, err := k.Kind(); err != nil {
return fmt.Errorf("%s: %w", k, err)
return xerrors.Errorf("%s: %w", k, err)
}
}

Expand All @@ -346,7 +346,7 @@ func (er EnforcementRules) Validate() error {
}
for _, v := range er {
if _, ok := validPenalties[v]; !ok {
return fmt.Errorf("%s: unknown penalty", v)
return xerrors.Errorf("%s: unknown penalty", v)
}
}

Expand Down Expand Up @@ -803,7 +803,7 @@ func getWorkspaceFromProcess(tid int) (res *InfringingWorkspace, err error) {
}
}
if supervisor.PID == 0 || workspacekit.PID == 0 {
return nil, fmt.Errorf("did not find supervisor or workspacekit parent")
return nil, xerrors.Errorf("did not find supervisor or workspacekit parent")
}

env, err := workspacekit.Environ()
Expand Down