From 5f3f04a55138f35afe1451627e5c0ee44870b8f7 Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 31 Jul 2024 10:43:21 -0400 Subject: [PATCH] Error types should satisfy error interface instead of embedding error field. --- internal/runbits/checkout/checkout.go | 2 +- internal/runbits/checkout/path.go | 12 +++++++++--- .../runbits/runtime/requirements/requirements.go | 7 +++++-- internal/runners/artifacts/download.go | 7 +++++-- internal/runners/initialize/init.go | 10 +++++----- internal/runners/pull/pull.go | 6 ++++-- internal/runners/push/push.go | 14 ++++++++++---- pkg/localcommit/localcommit.go | 7 +++++-- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/internal/runbits/checkout/checkout.go b/internal/runbits/checkout/checkout.go index 9e6acf84a7..23d3b4b139 100644 --- a/internal/runbits/checkout/checkout.go +++ b/internal/runbits/checkout/checkout.go @@ -172,7 +172,7 @@ func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID, }) if err != nil { if osutils.IsAccessDeniedError(err) { - return &ErrNoPermission{err, checkoutPath} + return &ErrNoPermission{checkoutPath} } return errs.Wrap(err, "Could not create projectfile") } diff --git a/internal/runbits/checkout/path.go b/internal/runbits/checkout/path.go index 372c7ed89f..3d0c52e7ac 100644 --- a/internal/runbits/checkout/path.go +++ b/internal/runbits/checkout/path.go @@ -37,15 +37,21 @@ func (r *Checkout) pathToUse(namespace *project.Namespaced, preferredPath string } type ErrAlreadyCheckedOut struct { - error Path string } +func (e ErrAlreadyCheckedOut) Error() string { + return "already checked out" +} + type ErrNoPermission struct { - error Path string } +func (e ErrNoPermission) Error() string { + return "no permission" +} + func validatePath(ns *project.Namespaced, path string) error { if !fileutils.TargetExists(path) { return nil @@ -61,7 +67,7 @@ func validatePath(ns *project.Namespaced, path string) error { configFile := filepath.Join(path, constants.ConfigFileName) if fileutils.FileExists(configFile) { - return &ErrAlreadyCheckedOut{errs.New("already checked out at %s", path), path} + return &ErrAlreadyCheckedOut{path} } return nil diff --git a/internal/runbits/runtime/requirements/requirements.go b/internal/runbits/runtime/requirements/requirements.go index 2485d893ad..7a990fa99c 100644 --- a/internal/runbits/runtime/requirements/requirements.go +++ b/internal/runbits/runtime/requirements/requirements.go @@ -344,15 +344,18 @@ func (r *RequirementOperation) prepareBuildScript(bp *bpModel.BuildPlanner, pare } type ResolveNamespaceError struct { - error Name string } +func (e ResolveNamespaceError) Error() string { + return "unable to resolve namespace" +} + func (r *RequirementOperation) resolveNamespaces(ts *time.Time, requirements ...*Requirement) error { for _, requirement := range requirements { if err := r.resolveNamespace(ts, requirement); err != nil { if err != errNoLanguage { - err = &ResolveNamespaceError{err, requirement.Name} + err = errs.Pack(err, &ResolveNamespaceError{requirement.Name}) } return errs.Wrap(err, "Unable to resolve namespace") } diff --git a/internal/runners/artifacts/download.go b/internal/runners/artifacts/download.go index 5c363c8980..4558500a15 100644 --- a/internal/runners/artifacts/download.go +++ b/internal/runners/artifacts/download.go @@ -55,10 +55,13 @@ func NewDownload(prime primeable) *Download { } type errArtifactExists struct { - error Path string } +func (e errArtifactExists) Error() string { + return "artifact exists" +} + func rationalizeDownloadError(proj *project.Project, auth *authentication.Auth, err *error) { var artifactExistsErr *errArtifactExists @@ -137,7 +140,7 @@ func (d *Download) downloadArtifact(artifact *buildplan.Artifact, targetDir stri downloadPath := filepath.Join(targetDir, basename) if fileutils.TargetExists(downloadPath) { - return &errArtifactExists{Path: downloadPath} + return &errArtifactExists{downloadPath} } ctx, cancel := context.WithCancel(context.Background()) diff --git a/internal/runners/initialize/init.go b/internal/runners/initialize/init.go index c9abd4dc77..9b6ca9330a 100644 --- a/internal/runners/initialize/init.go +++ b/internal/runners/initialize/init.go @@ -73,10 +73,13 @@ type primeable interface { } type errProjectExists struct { - error path string } +func (e errProjectExists) Error() string { + return "project file already exists" +} + var errNoLanguage = errs.New("No language specified") type errUnrecognizedLanguage struct { @@ -156,10 +159,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) { } if fileutils.TargetExists(filepath.Join(path, constants.ConfigFileName)) { - return &errProjectExists{ - error: errs.New("Project file already exists"), - path: path, - } + return &errProjectExists{path} } err := fileutils.MkdirUnlessExists(path) diff --git a/internal/runners/pull/pull.go b/internal/runners/pull/pull.go index 3d2c0083ca..e409b0264a 100644 --- a/internal/runners/pull/pull.go +++ b/internal/runners/pull/pull.go @@ -45,11 +45,14 @@ type Pull struct { } type errNoCommonParent struct { - error localCommitID strfmt.UUID remoteCommitID strfmt.UUID } +func (e errNoCommonParent) Error() string { + return "no common parent" +} + type PullParams struct { Force bool } @@ -140,7 +143,6 @@ func (p *Pull) Run(params *PullParams) (rerr error) { if commonParent == nil { return &errNoCommonParent{ - errs.New("no common parent"), *localCommit, *remoteCommit, } diff --git a/internal/runners/push/push.go b/internal/runners/push/push.go index aff49295ac..812d899173 100644 --- a/internal/runners/push/push.go +++ b/internal/runners/push/push.go @@ -68,15 +68,21 @@ var ( ) type errProjectNameInUse struct { - error Namespace *project.Namespaced } +func (e errProjectNameInUse) Error() string { + return "project name in use" +} + type errHeadless struct { - error ProjectURL string } +func (e errHeadless) Error() string { + return "headless project" +} + func (r *Push) Run(params PushParams) (rerr error) { defer rationalizeError(&rerr) @@ -106,7 +112,7 @@ func (r *Push) Run(params PushParams) (rerr error) { } if r.project.IsHeadless() { - return &errHeadless{err, r.project.URL()} + return &errHeadless{r.project.URL()} } // Capture the primary intend of the user @@ -154,7 +160,7 @@ func (r *Push) Run(params PushParams) (rerr error) { var projectCreated bool if intend&intendCreateProject > 0 || targetPjm == nil { if targetPjm != nil { - return &errProjectNameInUse{errs.New("project name in use"), targetNamespace} + return &errProjectNameInUse{targetNamespace} } // If the user didn't necessarily intend to create the project we should ask them for confirmation diff --git a/pkg/localcommit/localcommit.go b/pkg/localcommit/localcommit.go index dcd1c15532..9ef8a3e516 100644 --- a/pkg/localcommit/localcommit.go +++ b/pkg/localcommit/localcommit.go @@ -15,10 +15,13 @@ import ( var proj *project.Project type ErrInvalidCommitID struct { - error CommitID string } +func (e ErrInvalidCommitID) Error() string { + return "invalid commit ID" +} + func setupProject(pjpath string) error { if proj != nil && proj.Dir() == pjpath { return nil @@ -38,7 +41,7 @@ func Get(pjpath string) (strfmt.UUID, error) { commitID := proj.LegacyCommitID() if !strfmt.IsUUID(commitID) { - return "", &ErrInvalidCommitID{errs.New("Invalid commit ID"), commitID} + return "", &ErrInvalidCommitID{commitID} } return strfmt.UUID(commitID), nil