Skip to content

Commit

Permalink
Log when parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha committed Jan 28, 2021
1 parent 953f68d commit 19a74df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/cli/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func check(err error) {
os.Exit(1)
}

if _, ok := err.(*logs.ErrorInvalidWhenExpression); ok {
os.Exit(1)
}

panic(err)
}

Expand Down
34 changes: 33 additions & 1 deletion pkg/pipelines/extract_when_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pipelines
import (
"strconv"

logs "github.com/semaphoreci/spc/pkg/logs"
when "github.com/semaphoreci/spc/pkg/when"
whencli "github.com/semaphoreci/spc/pkg/when/whencli"
)
Expand All @@ -29,9 +30,16 @@ func (e *whenExtractor) Parse() ([]when.WhenExpression, error) {
expressions = append(expressions, e.Expression)
}

res := []when.WhenExpression{}

requirments, err := whencli.ListInputs(expressions)
if err != nil {
return []when.WhenExpression{}, err
return res, err
}

err = e.verifyParsed(requirments)
if err != nil {
return res, err
}

for index := range e.list {
Expand All @@ -41,6 +49,30 @@ func (e *whenExtractor) Parse() ([]when.WhenExpression, error) {
return e.list, nil
}

func (e *whenExtractor) verifyParsed(requirments []whencli.ListInputsResult) error {
var err error

for index, r := range requirments {
if r.Error != "" {
loc := logs.Location{
Path: e.list[index].Path,
File: e.pipeline.yamlPath,
}

logError := logs.ErrorInvalidWhenExpression{
Message: r.Error,
Location: loc,
}

logs.Log(logError)

err = &logError
}
}

return err
}

func (e *whenExtractor) ExtractAutoCancel() {
e.tryExtractingFromPath([]string{"auto_cancel", "queued", "when"})
e.tryExtractingFromPath([]string{"auto_cancel", "running", "when"})
Expand Down
3 changes: 0 additions & 3 deletions pkg/when/whencli/list_inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"

Expand Down Expand Up @@ -51,8 +50,6 @@ func prepareResults(expressions []string, results *gabs.Container) ([]ListInputs
Inputs: el.Search("inputs"),
Error: el.Search("error").Data().(string),
})
log.Println(index)
log.Println(el)
}

return result, nil
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/change_in_invalid_when.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

assert_eq(errors[0], {
"type" => "ErrorInvalidWhenExpression",
"message" => "Invalid when expression: branch = 'master' and ahahahaha and change_in('/lib')",
"message" => "Invalid expression on the left of 'and' operator.",
"location" => {
"file" => ".semaphore/semaphore.yml",
"path" => ["blocks", "0", "skip", "when"]
Expand All @@ -63,7 +63,7 @@

assert_eq(errors[1], {
"type" => "ErrorInvalidWhenExpression",
"message" => "Invalid when expression: branch =",
"message" => "Invalid or incomplete expression at the end of the line.",
"location" => {
"file" => ".semaphore/semaphore.yml",
"path" => ["blocks", "1", "skip", "when"]
Expand Down

0 comments on commit 19a74df

Please sign in to comment.