53 return buf.String() |
54 return buf.String() |
54 } |
55 } |
55 |
56 |
56 type Key []string |
57 type Key []string |
57 |
58 |
58 // internal version of DecodeError that is used as the base to create a |
|
59 // DecodeError with full context. |
|
60 type decodeError struct { |
|
61 highlight []byte |
|
62 message string |
|
63 key Key // optional |
|
64 } |
|
65 |
|
66 func (de *decodeError) Error() string { |
|
67 return de.message |
|
68 } |
|
69 |
|
70 func newDecodeError(highlight []byte, format string, args ...interface{}) error { |
|
71 return &decodeError{ |
|
72 highlight: highlight, |
|
73 message: fmt.Errorf(format, args...).Error(), |
|
74 } |
|
75 } |
|
76 |
|
77 // Error returns the error message contained in the DecodeError. |
59 // Error returns the error message contained in the DecodeError. |
78 func (e *DecodeError) Error() string { |
60 func (e *DecodeError) Error() string { |
79 return "toml: " + e.message |
61 return "toml: " + e.message |
80 } |
62 } |
81 |
63 |
103 // |
85 // |
104 // The function copies all bytes used in DecodeError, so that document and |
86 // The function copies all bytes used in DecodeError, so that document and |
105 // highlight can be freely deallocated. |
87 // highlight can be freely deallocated. |
106 // |
88 // |
107 //nolint:funlen |
89 //nolint:funlen |
108 func wrapDecodeError(document []byte, de *decodeError) *DecodeError { |
90 func wrapDecodeError(document []byte, de *unstable.ParserError) *DecodeError { |
109 offset := danger.SubsliceOffset(document, de.highlight) |
91 offset := danger.SubsliceOffset(document, de.Highlight) |
110 |
92 |
111 errMessage := de.Error() |
93 errMessage := de.Error() |
112 errLine, errColumn := positionAtEnd(document[:offset]) |
94 errLine, errColumn := positionAtEnd(document[:offset]) |
113 before, after := linesOfContext(document, de.highlight, offset, 3) |
95 before, after := linesOfContext(document, de.Highlight, offset, 3) |
114 |
96 |
115 var buf strings.Builder |
97 var buf strings.Builder |
116 |
98 |
117 maxLine := errLine + len(after) - 1 |
99 maxLine := errLine + len(after) - 1 |
118 lineColumnWidth := len(strconv.Itoa(maxLine)) |
100 lineColumnWidth := len(strconv.Itoa(maxLine)) |
156 |
138 |
157 if len(before) > 0 { |
139 if len(before) > 0 { |
158 buf.WriteString(strings.Repeat(" ", len(before[0]))) |
140 buf.WriteString(strings.Repeat(" ", len(before[0]))) |
159 } |
141 } |
160 |
142 |
161 buf.WriteString(strings.Repeat("~", len(de.highlight))) |
143 buf.WriteString(strings.Repeat("~", len(de.Highlight))) |
162 |
144 |
163 if len(errMessage) > 0 { |
145 if len(errMessage) > 0 { |
164 buf.WriteString(" ") |
146 buf.WriteString(" ") |
165 buf.WriteString(errMessage) |
147 buf.WriteString(errMessage) |
166 } |
148 } |