Skip to content

Commit 6474baf

Browse files
committed
refactor yaml parse error message
1 parent c0c641e commit 6474baf

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

cli/error.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,15 @@ type yamlParseError struct {
124124

125125
func (err *yamlParseError) Error() string {
126126
var line int
127-
msg := err.err.Error()
128-
fmt.Sscanf(msg, "yaml: line %d:", &line)
129-
if line > 0 {
130-
msg = msg[7+strings.IndexRune(msg[5:], ':'):] // trim "yaml: line N:"
131-
} else {
132-
if !strings.HasPrefix(msg, "yaml: unmarshal errors:\n") {
133-
return "invalid yaml: " + err.fname
134-
}
135-
msg = strings.Split(msg, "\n")[1]
136-
fmt.Sscanf(msg, " line %d: ", &line)
137-
if line == 0 {
138-
return "invalid yaml: " + err.fname
139-
}
140-
msg = msg[2+strings.IndexRune(msg, ':'):] // trim "line N:"
127+
msg := strings.TrimPrefix(
128+
strings.TrimPrefix(err.err.Error(), "yaml: "),
129+
"unmarshal errors:\n ")
130+
if fmt.Sscanf(msg, "line %d: ", &line); line == 0 {
131+
return "invalid yaml: " + err.fname
132+
}
133+
msg = msg[strings.Index(msg, ": ")+2:]
134+
if i := strings.IndexByte(msg, '\n'); i >= 0 {
135+
msg = msg[:i]
141136
}
142137
linestr := getLineByLine(err.contents, line)
143138
return fmt.Sprintf("invalid yaml: %s:%d\n%s %s",

0 commit comments

Comments
 (0)