FormatYAMLError formats a YAML error with source code context using yaml.FormatError() frontmatterLineOffset is the line number where the frontmatter content begins in the document (1-based) Returns the formatted error string with line numbers adjusted for frontmatter position
(err error, frontmatterLineOffset int, sourceYAML string)
| 137 | // frontmatterLineOffset is the line number where the frontmatter content begins in the document (1-based) |
| 138 | // Returns the formatted error string with line numbers adjusted for frontmatter position |
| 139 | func FormatYAMLError(err error, frontmatterLineOffset int, sourceYAML string) string { |
| 140 | yamlErrorLog.Printf("Formatting YAML error with yaml.FormatError(): offset=%d", frontmatterLineOffset) |
| 141 | |
| 142 | // Use goccy/go-yaml's native FormatError for consistent formatting with source context |
| 143 | // colored=false to avoid ANSI escape codes, inclSource=true to include source lines |
| 144 | formatted := yaml.FormatError(err, false, true) |
| 145 | |
| 146 | // Translate cryptic parser messages to user-friendly descriptions (header line only) |
| 147 | formatted = translateYAMLError(formatted) |
| 148 | |
| 149 | // Adjust line numbers in the formatted output to account for frontmatter position |
| 150 | if frontmatterLineOffset > 1 { |
| 151 | formatted = adjustLineNumbersInFormattedError(formatted, frontmatterLineOffset-1) |
| 152 | } |
| 153 | |
| 154 | return formatted |
| 155 | } |
| 156 | |
| 157 | // adjustLineNumbersInFormattedError adjusts line numbers in yaml.FormatError() output |
| 158 | // by adding the specified offset to all line numbers |