MCPcopy Create free account
hub / github.com/github/gh-aw / translateYAMLError

Function translateYAMLError

pkg/parser/yaml_error.go:102–134  ·  view source on GitHub ↗

translateYAMLError translates cryptic goccy/go-yaml parser messages to user-friendly descriptions. It operates on the full yaml.FormatError() output, which includes a header line and source context: [line:col] original parser message > 1 | some: yaml ^ Only the parser message portion (t

(formatted string)

Source from the content-addressed store, hash-verified

100// Only the parser message portion (the header line, after the "[line:col] " prefix) is translated.
101// Source-context lines are left untouched to avoid accidentally replacing text inside user YAML content.
102func translateYAMLError(formatted string) string {
103 if formatted == "" {
104 return formatted
105 }
106
107 // Split into the header line (which contains the parser message) and the rest (source context).
108 var header, rest string
109 if nl := strings.IndexByte(formatted, '\n'); nl >= 0 {
110 header = formatted[:nl]
111 rest = formatted[nl:]
112 } else {
113 header = formatted
114 rest = ""
115 }
116
117 // Within the header, locate the parser message text after the "[line:col] " prefix.
118 // If the prefix is absent (unusual), treat the entire header as the message.
119 msgStart := strings.Index(header, "] ")
120 var prefix, msg string
121 if msgStart >= 0 {
122 msgStart += len("] ")
123 prefix = header[:msgStart]
124 msg = header[msgStart:]
125 } else {
126 prefix = ""
127 msg = header
128 }
129
130 // Translate only the message portion, leaving prefix and source context intact.
131 translated := TranslateYAMLMessage(msg)
132
133 return prefix + translated + rest
134}
135
136// FormatYAMLError formats a YAML error with source code context using yaml.FormatError()
137// frontmatterLineOffset is the line number where the frontmatter content begins in the document (1-based)

Callers 2

TestTranslateYAMLErrorFunction · 0.85
FormatYAMLErrorFunction · 0.85

Calls 1

TranslateYAMLMessageFunction · 0.85

Tested by 1

TestTranslateYAMLErrorFunction · 0.68