Skip to content

Commit ae29e45

Browse files
authored
Merge pull request #2253 from FabianKramm/main
fix: improve unmarshal error
2 parents 88d556b + 0b9bc10 commit ae29e45

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pkg/devspace/config/versions/validate.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ func Validate(config *latest.Config) error {
9191
return err
9292
}
9393

94+
err = validateFunctions(config.Functions)
95+
if err != nil {
96+
return err
97+
}
98+
99+
return nil
100+
}
101+
102+
func isReservedFunctionName(name string) bool {
103+
switch name {
104+
case "true", ":", "false", "exit", "set", "shift", "unset",
105+
"echo", "printf", "break", "continue", "pwd", "cd",
106+
"wait", "builtin", "trap", "type", "source", ".", "command",
107+
"dirs", "pushd", "popd", "umask", "alias", "unalias",
108+
"fg", "bg", "getopts", "eval", "test", "devspace", "[", "exec",
109+
"return", "read", "shopt":
110+
return true
111+
}
112+
return false
113+
}
114+
115+
func validateFunctions(functions map[string]string) error {
116+
for name := range functions {
117+
if isReservedFunctionName(name) {
118+
return fmt.Errorf("you cannot use '%s' as a function name as its an internally used special function. Please choose another name", name)
119+
}
120+
}
121+
94122
return nil
95123
}
96124

pkg/util/yamlutil/yaml.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ func Unmarshal(data []byte, out interface{}) error {
4343
func prettifyError(data []byte, err error) error {
4444
// check if type error
4545
if typeError, ok := err.(*yaml.TypeError); ok {
46+
// print the config with lines
47+
lines := strings.Split(string(data), "\n")
48+
extraLines := []string{"Parsed Config:"}
49+
for i, v := range lines {
50+
if v == "" {
51+
continue
52+
}
53+
extraLines = append(extraLines, fmt.Sprintf(" %d: %s", i+1, v))
54+
}
55+
extraLines = append(extraLines, "Errors:")
56+
4657
for i := range typeError.Errors {
4758
typeError.Errors[i] = strings.ReplaceAll(typeError.Errors[i], "!!seq", "an array")
4859
typeError.Errors[i] = strings.ReplaceAll(typeError.Errors[i], "!!str", "string")
@@ -58,11 +69,14 @@ func prettifyError(data []byte, err error) error {
5869
line = line - 1
5970
lines := strings.Split(string(data), "\n")
6071
if line < len(lines) {
61-
typeError.Errors[i] += fmt.Sprintf(" (line %d: %s)", line+1, strings.TrimSpace(lines[line]))
72+
typeError.Errors[i] = " " + typeError.Errors[i] + fmt.Sprintf(" (line %d: %s)", line+1, strings.TrimSpace(lines[line]))
6273
}
6374
}
6475
}
6576
}
77+
78+
extraLines = append(extraLines, typeError.Errors...)
79+
typeError.Errors = extraLines
6680
}
6781

6882
return err

0 commit comments

Comments
 (0)