From b92bfb53d259b7f8acf6ec025d94f3780e859eb5 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 12 Jul 2024 20:56:58 -0700 Subject: [PATCH] update errors handling allocations and comparison Signed-off-by: Tonis Tiigi --- bake/hclparser/hclparser.go | 17 +++++++++++------ build/build.go | 5 ----- build/opt.go | 4 ++-- driver/docker/driver.go | 4 ++-- driver/driver.go | 15 ++++++++++++--- driver/kubernetes/manifest/manifest.go | 20 +++++++++++++------- 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/bake/hclparser/hclparser.go b/bake/hclparser/hclparser.go index eee3a670..fe7dc772 100644 --- a/bake/hclparser/hclparser.go +++ b/bake/hclparser/hclparser.go @@ -75,7 +75,12 @@ type WithGetName interface { GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(hcl.Expression) hcl.Diagnostics) (string, error) } -var errUndefined = errors.New("undefined") +// errUndefined is returned when a variable or function is not defined. +type errUndefined struct{} + +func (errUndefined) Error() string { + return "undefined" +} func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map[string]struct{}, allowMissing bool) hcl.Diagnostics { fns, hcldiags := funcCalls(exp) @@ -85,7 +90,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map for _, fn := range fns { if err := p.resolveFunction(ectx, fn); err != nil { - if allowMissing && errors.Is(err, errUndefined) { + if allowMissing && errors.Is(err, errUndefined{}) { continue } return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) @@ -139,7 +144,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map } for _, block := range blocks { if err := p.resolveBlock(block, target); err != nil { - if allowMissing && errors.Is(err, errUndefined) { + if allowMissing && errors.Is(err, errUndefined{}) { continue } return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) @@ -147,7 +152,7 @@ func (p *parser) loadDeps(ectx *hcl.EvalContext, exp hcl.Expression, exclude map } } else { if err := p.resolveValue(ectx, v.RootName()); err != nil { - if allowMissing && errors.Is(err, errUndefined) { + if allowMissing && errors.Is(err, errUndefined{}) { continue } return wrapErrorDiagnostic("Invalid expression", err, exp.Range().Ptr(), exp.Range().Ptr()) @@ -169,7 +174,7 @@ func (p *parser) resolveFunction(ectx *hcl.EvalContext, name string) error { } f, ok := p.funcs[name] if !ok { - return errors.Wrapf(errUndefined, "function %q does not exist", name) + return errors.Wrapf(errUndefined{}, "function %q does not exist", name) } if _, ok := p.progressF[key(ectx, name)]; ok { return errors.Errorf("function cycle not allowed for %s", name) @@ -259,7 +264,7 @@ func (p *parser) resolveValue(ectx *hcl.EvalContext, name string) (err error) { if _, builtin := p.opt.Vars[name]; !ok && !builtin { vr, ok := p.vars[name] if !ok { - return errors.Wrapf(errUndefined, "variable %q does not exist", name) + return errors.Wrapf(errUndefined{}, "variable %q does not exist", name) } def = vr.Default ectx = p.ectx diff --git a/build/build.go b/build/build.go index 34a895d4..3dec9588 100644 --- a/build/build.go +++ b/build/build.go @@ -48,11 +48,6 @@ import ( "golang.org/x/sync/errgroup" ) -var ( - errStdinConflict = errors.New("invalid argument: can't use stdin for both build context and dockerfile") - errDockerfileConflict = errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles") -) - const ( printFallbackImage = "docker/dockerfile:1.5@sha256:dbbd5e059e8a07ff7ea6233b213b36aa516b4c53c645f1817a4dd18b83cbea56" printLintFallbackImage = "docker.io/docker/dockerfile-upstream:1.8.1@sha256:e87caa74dcb7d46cd820352bfea12591f3dba3ddc4285e19c7dcd13359f7cefd" diff --git a/build/opt.go b/build/opt.go index 0d6dfbef..a830e698 100644 --- a/build/opt.go +++ b/build/opt.go @@ -379,7 +379,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, addVCSL target.FrontendInputs["dockerfile"] = *inp.ContextState case inp.ContextPath == "-": if inp.DockerfilePath == "-" { - return nil, errStdinConflict + return nil, errors.Errorf("invalid argument: can't use stdin for both build context and dockerfile") } buf := bufio.NewReader(inp.InStream) @@ -395,7 +395,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, addVCSL target.Session = append(target.Session, up) } else { if inp.DockerfilePath != "" { - return nil, errDockerfileConflict + return nil, errors.Errorf("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles") } // stdin is dockerfile dockerfileReader = buf diff --git a/driver/docker/driver.go b/driver/docker/driver.go index 2b1481f6..d9d278e7 100644 --- a/driver/docker/driver.go +++ b/driver/docker/driver.go @@ -29,7 +29,7 @@ func (d *Driver) Bootstrap(ctx context.Context, l progress.Logger) error { func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { _, err := d.DockerAPI.ServerVersion(ctx) if err != nil { - return nil, errors.Wrapf(driver.ErrNotConnecting, err.Error()) + return nil, errors.Wrapf(driver.ErrNotConnecting{}, err.Error()) } return &driver.Info{ Status: driver.Running, @@ -39,7 +39,7 @@ func (d *Driver) Info(ctx context.Context) (*driver.Info, error) { func (d *Driver) Version(ctx context.Context) (string, error) { v, err := d.DockerAPI.ServerVersion(ctx) if err != nil { - return "", errors.Wrapf(driver.ErrNotConnecting, err.Error()) + return "", errors.Wrapf(driver.ErrNotConnecting{}, err.Error()) } if bkversion, _ := resolveBuildKitVersion(v.Version); bkversion != "" { return bkversion, nil diff --git a/driver/driver.go b/driver/driver.go index ec88f1a4..c82852cd 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -14,8 +14,17 @@ import ( "github.com/pkg/errors" ) -var ErrNotRunning = errors.Errorf("driver not running") -var ErrNotConnecting = errors.Errorf("driver not connecting") +type ErrNotRunning struct{} + +func (ErrNotRunning) Error() string { + return "driver not running" +} + +type ErrNotConnecting struct{} + +func (ErrNotConnecting) Error() string { + return "driver not connecting" +} type Status int @@ -105,7 +114,7 @@ func Boot(ctx, clientContext context.Context, d *DriverHandle, pw progress.Write c, err := d.Client(clientContext) if err != nil { - if errors.Cause(err) == ErrNotRunning && try <= 2 { + if errors.Is(err, ErrNotRunning{}) && try <= 2 { continue } return nil, err diff --git a/driver/kubernetes/manifest/manifest.go b/driver/kubernetes/manifest/manifest.go index 7424d88e..1137badb 100644 --- a/driver/kubernetes/manifest/manifest.go +++ b/driver/kubernetes/manifest/manifest.go @@ -7,7 +7,6 @@ import ( "github.com/docker/buildx/util/platformutil" v1 "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -53,10 +52,17 @@ const ( LabelApp = "app" ) -var ( - ErrReservedAnnotationPlatform = errors.Errorf("the annotation \"%s\" is reserved and cannot be customized", AnnotationPlatform) - ErrReservedLabelApp = errors.Errorf("the label \"%s\" is reserved and cannot be customized", LabelApp) -) +type ErrReservedAnnotationPlatform struct{} + +func (ErrReservedAnnotationPlatform) Error() string { + return fmt.Sprintf("the annotation %q is reserved and cannot be customized", AnnotationPlatform) +} + +type ErrReservedLabelApp struct{} + +func (ErrReservedLabelApp) Error() string { + return fmt.Sprintf("the label %q is reserved and cannot be customized", LabelApp) +} func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.ConfigMap, err error) { labels := map[string]string{ @@ -73,14 +79,14 @@ func NewDeployment(opt *DeploymentOpt) (d *appsv1.Deployment, c []*corev1.Config for k, v := range opt.CustomAnnotations { if k == AnnotationPlatform { - return nil, nil, ErrReservedAnnotationPlatform + return nil, nil, ErrReservedAnnotationPlatform{} } annotations[k] = v } for k, v := range opt.CustomLabels { if k == LabelApp { - return nil, nil, ErrReservedLabelApp + return nil, nil, ErrReservedLabelApp{} } labels[k] = v }