Skip to content

Commit

Permalink
Merge pull request #2253 from FabianKramm/main
Browse files Browse the repository at this point in the history
fix: improve unmarshal error
  • Loading branch information
FabianKramm authored Aug 26, 2022
2 parents 88d556b + 0b9bc10 commit ae29e45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
28 changes: 28 additions & 0 deletions pkg/devspace/config/versions/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ func Validate(config *latest.Config) error {
return err
}

err = validateFunctions(config.Functions)
if err != nil {
return err
}

return nil
}

func isReservedFunctionName(name string) bool {
switch name {
case "true", ":", "false", "exit", "set", "shift", "unset",
"echo", "printf", "break", "continue", "pwd", "cd",
"wait", "builtin", "trap", "type", "source", ".", "command",
"dirs", "pushd", "popd", "umask", "alias", "unalias",
"fg", "bg", "getopts", "eval", "test", "devspace", "[", "exec",
"return", "read", "shopt":
return true
}
return false
}

func validateFunctions(functions map[string]string) error {
for name := range functions {
if isReservedFunctionName(name) {
return fmt.Errorf("you cannot use '%s' as a function name as its an internally used special function. Please choose another name", name)
}
}

return nil
}

Expand Down
16 changes: 15 additions & 1 deletion pkg/util/yamlutil/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ func Unmarshal(data []byte, out interface{}) error {
func prettifyError(data []byte, err error) error {
// check if type error
if typeError, ok := err.(*yaml.TypeError); ok {
// print the config with lines
lines := strings.Split(string(data), "\n")
extraLines := []string{"Parsed Config:"}
for i, v := range lines {
if v == "" {
continue
}
extraLines = append(extraLines, fmt.Sprintf(" %d: %s", i+1, v))
}
extraLines = append(extraLines, "Errors:")

for i := range typeError.Errors {
typeError.Errors[i] = strings.ReplaceAll(typeError.Errors[i], "!!seq", "an array")
typeError.Errors[i] = strings.ReplaceAll(typeError.Errors[i], "!!str", "string")
Expand All @@ -58,11 +69,14 @@ func prettifyError(data []byte, err error) error {
line = line - 1
lines := strings.Split(string(data), "\n")
if line < len(lines) {
typeError.Errors[i] += fmt.Sprintf(" (line %d: %s)", line+1, strings.TrimSpace(lines[line]))
typeError.Errors[i] = " " + typeError.Errors[i] + fmt.Sprintf(" (line %d: %s)", line+1, strings.TrimSpace(lines[line]))
}
}
}
}

extraLines = append(extraLines, typeError.Errors...)
typeError.Errors = extraLines
}

return err
Expand Down

0 comments on commit ae29e45

Please sign in to comment.