Browse Source

add descriptive error messages

Mohammed Nafees 3 years ago
parent
commit
3649939fa4

+ 9 - 13
api/server/handlers/environment/validate_porter_yaml.go

@@ -46,9 +46,9 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	request := &types.ValidatePorterYAMLRequest{}
+	req := &types.ValidatePorterYAMLRequest{}
 
-	if ok := c.DecodeAndValidate(w, r, request); !ok {
+	if ok := c.DecodeAndValidate(w, r, req); !ok {
 		return
 	}
 
@@ -73,7 +73,7 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 
 	res := &types.ValidatePorterYAMLResponse{}
 
-	if request.Branch == "" { // get the default branch name
+	if req.Branch == "" { // get the default branch name
 		repo, _, err := ghClient.Repositories.Get(r.Context(), env.GitRepoOwner, env.GitRepoName)
 
 		if err != nil {
@@ -81,18 +81,18 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 			return
 		}
 
-		request.Branch = repo.GetDefaultBranch()
+		req.Branch = repo.GetDefaultBranch()
 	}
 
 	fileContents, _, ghResp, err := ghClient.Repositories.GetContents(
 		context.Background(), env.GitRepoOwner, env.GitRepoName, "porter.yaml",
 		&github.RepositoryContentGetOptions{
-			Ref: request.Branch,
+			Ref: req.Branch,
 		},
 	)
 
 	if ghResp.StatusCode == 404 {
-		res.Errors = append(res.Errors, "no porter.yaml file found in the root of the repository")
+		res.Errors = append(res.Errors, preview.ErrNoPorterYAMLFile)
 		c.WriteResult(w, r, res)
 		return
 	}
@@ -110,18 +110,14 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 	}
 
 	if contents == "" {
-		res.Errors = append(res.Errors, "porter.yaml file is empty")
+		res.Errors = append(res.Errors, preview.ErrEmptyPorterYAMLFile)
 		c.WriteResult(w, r, res)
 		return
 	}
 
 	validator := preview.NewPorterYAMLValidator()
 
-	err = validator.Validate(contents)
+	res.Errors = append(res.Errors, validator.Validate(contents)...)
 
-	if err != nil {
-		res.Errors = append(res.Errors, err.Error())
-		c.WriteResult(w, r, res)
-		return
-	}
+	c.WriteResult(w, r, res)
 }

+ 1 - 1
api/types/environment.go

@@ -135,5 +135,5 @@ type ValidatePorterYAMLRequest struct {
 }
 
 type ValidatePorterYAMLResponse struct {
-	Errors []string `json:"errors"`
+	Errors []error `json:"errors,omitempty"`
 }

+ 7 - 0
internal/integrations/preview/push_image_driver_validator.go

@@ -0,0 +1,7 @@
+package preview
+
+import "github.com/porter-dev/switchboard/pkg/types"
+
+func pushImageDriverValidator(resource *types.Resource) error {
+	return nil
+}

+ 31 - 8
internal/integrations/preview/validate.go

@@ -1,32 +1,55 @@
 package preview
 
 import (
-	"github.com/porter-dev/switchboard/pkg/models"
+	"errors"
+	"fmt"
+
 	"github.com/porter-dev/switchboard/pkg/parser"
+	"github.com/porter-dev/switchboard/pkg/types"
+)
+
+var (
+	ErrNoPorterYAMLFile    = errors.New("porter.yaml does not exist in the root of this repository")
+	ErrEmptyPorterYAMLFile = errors.New("porter.yaml is empty")
+
+	ErrUnsupportedDriver = errors.New("no such driver")
 )
 
-type driverBasedResourceValidator func(*models.Resource)
+type driverBasedResourceValidator func(*types.Resource) error
 
 type porterYAMLValidator struct {
 	driverValidators map[string]driverBasedResourceValidator
 }
 
 func NewPorterYAMLValidator() *porterYAMLValidator {
+	driverValidators := make(map[string]driverBasedResourceValidator)
+
+	driverValidators["push-image"] = pushImageDriverValidator
+
 	return &porterYAMLValidator{
-		driverValidators: make(map[string]driverBasedResourceValidator),
+		driverValidators: driverValidators,
 	}
 }
 
-func (v *porterYAMLValidator) Validate(contents string) error {
+func (v *porterYAMLValidator) Validate(contents string) []error {
+	var errors []error
+
 	resGroup, err := parser.ParseRawBytes([]byte(contents))
 
 	if err != nil {
-		return err
+		errors = append(errors, fmt.Errorf("error parsing porter.yaml: %w", err))
+		return errors
 	}
 
-	for range resGroup.Resources {
-
+	for _, res := range resGroup.Resources {
+		if validator, ok := v.driverValidators[res.Driver]; ok {
+			if err := validator(res); err != nil {
+				errors = append(errors, err)
+			}
+		} else {
+			errors = append(errors, fmt.Errorf("%w: %s", ErrUnsupportedDriver, res.Driver))
+		}
 	}
 
-	return nil
+	return errors
 }