Skip to content

Commit da620cd

Browse files
authored
New Project/StarterProject Error Types (#614)
* New Error Tests * Update error message
1 parent 96ab67b commit da620cd

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

pkg/validation/errors.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,52 @@ func (e *InvalidComponentError) Error() string {
111111
return fmt.Sprintf("the component %q is invalid - %s", e.componentName, e.reason)
112112
}
113113

114+
//MissingProjectRemoteError returns an error if the git remotes object under a project is empty
115+
type MissingProjectRemoteError struct {
116+
projectName string
117+
}
118+
119+
func (e *MissingProjectRemoteError) Error() string {
120+
return fmt.Sprintf("project %s should have at least one remote", e.projectName)
121+
}
122+
123+
//MissingStarterProjectRemoteError returns an error if the git remotes object under a starterProject is empty
124+
type MissingStarterProjectRemoteError struct {
125+
projectName string
126+
}
127+
128+
func (e *MissingStarterProjectRemoteError) Error() string {
129+
return fmt.Sprintf("starterProject %s should have at least one remote", e.projectName)
130+
}
131+
132+
//MultipleStarterProjectRemoteError returns an error if multiple git remotes are specified. There can only be one remote.
133+
type MultipleStarterProjectRemoteError struct {
134+
projectName string
135+
}
136+
137+
func (e *MultipleStarterProjectRemoteError) Error() string {
138+
return fmt.Sprintf("starterProject %s should have one remote only", e.projectName)
139+
}
140+
141+
//MissingProjectCheckoutFromRemoteError returns an error if there are multiple git remotes but the checkoutFrom remote has not been specified
142+
type MissingProjectCheckoutFromRemoteError struct {
143+
projectName string
144+
}
145+
146+
func (e *MissingProjectCheckoutFromRemoteError) Error() string {
147+
return fmt.Sprintf("project %s has more than one remote defined, but has no checkoutfrom remote defined", e.projectName)
148+
}
149+
150+
//InvalidProjectCheckoutRemoteError returns an error if there is an unmatched, checkoutFrom remote specified
151+
type InvalidProjectCheckoutRemoteError struct {
152+
projectName string
153+
checkoutRemote string
154+
}
155+
156+
func (e *InvalidProjectCheckoutRemoteError) Error() string {
157+
return fmt.Sprintf("unable to find the checkout remote %s in the remotes for project %s", e.checkoutRemote, e.projectName)
158+
}
159+
114160
// resolveErrorMessageWithImportAttributes returns an updated error message
115161
// with detailed information on the imported and overriden resource.
116162
// example:

pkg/validation/projects.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package validation
22

33
import (
4-
"fmt"
54
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
65
"github.com/hashicorp/go-multierror"
76
)
87

98
// ValidateStarterProjects checks if starter project has only one remote configured
10-
// and if the checkout remote matches the renote configured
9+
// and if the checkout remote matches the remote configured
1110
func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returnedErr error) {
1211

1312
for _, starterProject := range starterProjects {
@@ -20,8 +19,8 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returne
2019

2120
switch len(gitSource.Remotes) {
2221
case 0:
23-
starterProjectErr := fmt.Errorf("starterProject %s should have at least one remote", starterProject.Name)
24-
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)
22+
23+
newErr := resolveErrorMessageWithImportAttributes(&MissingStarterProjectRemoteError{projectName: starterProject.Name}, starterProject.Attributes)
2524
returnedErr = multierror.Append(returnedErr, newErr)
2625
case 1:
2726
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
@@ -32,8 +31,8 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returne
3231
}
3332
}
3433
default: // len(gitSource.Remotes) >= 2
35-
starterProjectErr := fmt.Errorf("starterProject %s should have one remote only", starterProject.Name)
36-
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)
34+
35+
newErr := resolveErrorMessageWithImportAttributes(&MultipleStarterProjectRemoteError{projectName: starterProject.Name}, starterProject.Attributes)
3736
returnedErr = multierror.Append(returnedErr, newErr)
3837
}
3938
}
@@ -54,8 +53,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
5453
}
5554
switch len(gitSource.Remotes) {
5655
case 0:
57-
projectErr := fmt.Errorf("projects %s should have at least one remote", project.Name)
58-
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)
56+
57+
newErr := resolveErrorMessageWithImportAttributes(&MissingProjectRemoteError{projectName: project.Name}, project.Attributes)
5958
returnedErr = multierror.Append(returnedErr, newErr)
6059
case 1:
6160
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
@@ -66,8 +65,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
6665
}
6766
default: // len(gitSource.Remotes) >= 2
6867
if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" {
69-
projectErr := fmt.Errorf("project %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
70-
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)
68+
69+
newErr := resolveErrorMessageWithImportAttributes(&MissingProjectCheckoutFromRemoteError{projectName: project.Name}, project.Attributes)
7170
returnedErr = multierror.Append(returnedErr, newErr)
7271
continue
7372
}
@@ -85,7 +84,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
8584
func validateRemoteMap(remotes map[string]string, checkoutRemote, projectName string) error {
8685

8786
if _, ok := remotes[checkoutRemote]; !ok {
88-
return fmt.Errorf("unable to find the checkout remote %s in the remotes for project %s", checkoutRemote, projectName)
87+
88+
return &InvalidProjectCheckoutRemoteError{projectName: projectName, checkoutRemote: checkoutRemote}
8989
}
9090

9191
return nil

pkg/validation/projects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestValidateStarterProjects(t *testing.T) {
123123
func TestValidateProjects(t *testing.T) {
124124

125125
wrongCheckoutErr := "unable to find the checkout remote .* in the remotes for project.*"
126-
atleastOneRemoteErr := "projects .* should have at least one remote"
126+
atleastOneRemoteErr := "project .* should have at least one remote"
127127
missingCheckOutFromRemoteErr := "project .* has more than one remote defined, but has no checkoutfrom remote defined"
128128

129129
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,

0 commit comments

Comments
 (0)