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

Error types should satisfy error interface instead of embedding error field. #3425

Merged
merged 1 commit into from
Jul 31, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/runbits/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
12 changes: 9 additions & 3 deletions internal/runbits/checkout/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions internal/runbits/runtime/requirements/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
7 changes: 5 additions & 2 deletions internal/runners/artifacts/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions internal/runners/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions internal/runners/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -140,7 +143,6 @@ func (p *Pull) Run(params *PullParams) (rerr error) {

if commonParent == nil {
return &errNoCommonParent{
errs.New("no common parent"),
*localCommit,
*remoteCommit,
}
Expand Down
14 changes: 10 additions & 4 deletions internal/runners/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pkg/localcommit/localcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading