Skip to content

Commit 79b7cc5

Browse files
committed
fix: improve unmarshal error
1 parent 3a483cb commit 79b7cc5

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

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)