8
"github.com/pelletier/go-toml/v2/internal/danger"
9
"github.com/pelletier/go-toml/v2/unstable"
12
// DecodeError represents an error encountered during the parsing or decoding
15
// In addition to the error message, it contains the position in the document
16
// where it happened, as well as a human-readable representation that shows
17
// where the error occurred in the document.
18
type DecodeError struct {
27
// StrictMissingError occurs in a TOML document that does not have a
28
// corresponding field in the target value. It contains all the missing fields
31
// Emitted by Decoder when DisallowUnknownFields() was called.
32
type StrictMissingError struct {
33
// One error per field that could not be found.
37
// Error returns the canonical string for this error.
38
func (s *StrictMissingError) Error() string {
39
return "strict mode: fields in the document are missing in the target struct"
42
// String returns a human readable description of all errors.
43
func (s *StrictMissingError) String() string {
44
var buf strings.Builder
46
for i, e := range s.Errors {
48
buf.WriteString("\n---\n")
51
buf.WriteString(e.String())
59
// Error returns the error message contained in the DecodeError.
60
func (e *DecodeError) Error() string {
61
return "toml: " + e.message
64
// String returns the human-readable contextualized error. This string is multi-line.
65
func (e *DecodeError) String() string {
69
// Position returns the (line, column) pair indicating where the error
70
// occurred in the document. Positions are 1-indexed.
71
func (e *DecodeError) Position() (row int, column int) {
72
return e.line, e.column
75
// Key that was being processed when the error occurred. The key is present only
76
// if this DecodeError is part of a StrictMissingError.
77
func (e *DecodeError) Key() Key {
81
// decodeErrorFromHighlight creates a DecodeError referencing a highlighted
82
// range of bytes from document.
84
// highlight needs to be a sub-slice of document, or this function panics.
86
// The function copies all bytes used in DecodeError, so that document and
87
// highlight can be freely deallocated.
90
func wrapDecodeError(document []byte, de *unstable.ParserError) *DecodeError {
91
offset := danger.SubsliceOffset(document, de.Highlight)
93
errMessage := de.Error()
94
errLine, errColumn := positionAtEnd(document[:offset])
95
before, after := linesOfContext(document, de.Highlight, offset, 3)
97
var buf strings.Builder
99
maxLine := errLine + len(after) - 1
100
lineColumnWidth := len(strconv.Itoa(maxLine))
102
// Write the lines of context strictly before the error.
103
for i := len(before) - 1; i > 0; i-- {
105
buf.WriteString(formatLineNumber(line, lineColumnWidth))
108
if len(before[i]) > 0 {
116
// Write the document line that contains the error.
118
buf.WriteString(formatLineNumber(errLine, lineColumnWidth))
119
buf.WriteString("| ")
125
buf.Write(de.Highlight)
133
// Write the line with the error message itself (so it does not have a line
136
buf.WriteString(strings.Repeat(" ", lineColumnWidth))
137
buf.WriteString("| ")
140
buf.WriteString(strings.Repeat(" ", len(before[0])))
143
buf.WriteString(strings.Repeat("~", len(de.Highlight)))
145
if len(errMessage) > 0 {
147
buf.WriteString(errMessage)
150
// Write the lines of context strictly after the error.
152
for i := 1; i < len(after); i++ {
155
buf.WriteString(formatLineNumber(line, lineColumnWidth))
158
if len(after[i]) > 0 {
173
func formatLineNumber(line int, width int) string {
174
format := "%" + strconv.Itoa(width) + "d"
176
return fmt.Sprintf(format, line)
179
func linesOfContext(document []byte, highlight []byte, offset int, linesAround int) ([][]byte, [][]byte) {
180
return beforeLines(document, offset, linesAround), afterLines(document, highlight, offset, linesAround)
183
func beforeLines(document []byte, offset int, linesAround int) [][]byte {
184
var beforeLines [][]byte
186
// Walk the document backward from the highlight to find previous lines
188
rest := document[:offset]
190
for o := len(rest) - 1; o >= 0 && len(beforeLines) <= linesAround && len(rest) > 0; {
192
case rest[o] == '\n':
193
// handle individual lines
194
beforeLines = append(beforeLines, rest[o+1:])
198
// add the first line only if it's non-empty
199
beforeLines = append(beforeLines, rest)
210
func afterLines(document []byte, highlight []byte, offset int, linesAround int) [][]byte {
211
var afterLines [][]byte
213
// Walk the document forward from the highlight to find the following
215
rest := document[offset+len(highlight):]
217
for o := 0; o < len(rest) && len(afterLines) <= linesAround; {
219
case rest[o] == '\n':
220
// handle individual lines
221
afterLines = append(afterLines, rest[:o])
225
case o == len(rest)-1:
226
// add last line only if it's non-empty
227
afterLines = append(afterLines, rest)
238
func positionAtEnd(b []byte) (row int, column int) {
242
for _, c := range b {