File tree Expand file tree Collapse file tree 3 files changed +25
-5
lines changed Expand file tree Collapse file tree 3 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -121,13 +121,19 @@ func (p *parser) fail() {
121
121
line ++
122
122
}
123
123
}
124
+ var column int
125
+ if p .parser .context_mark .column != 0 {
126
+ column = p .parser .context_mark .column
127
+ } else if p .parser .problem_mark .column != 0 {
128
+ column = p .parser .problem_mark .column
129
+ }
124
130
var msg string
125
131
if len (p .parser .problem ) > 0 {
126
132
msg = p .parser .problem
127
133
} else {
128
134
msg = "unknown problem parsing YAML content"
129
135
}
130
- fail (& ParserError {msg , line })
136
+ fail (& ParserError {msg , line , column })
131
137
}
132
138
133
139
func (p * parser ) anchor (n * Node , anchor []byte ) {
Original file line number Diff line number Diff line change @@ -1145,19 +1145,32 @@ func TestDecoderErrors(t *testing.T) {
1145
1145
}
1146
1146
}
1147
1147
1148
- func TestParserError (t * testing.T ) {
1148
+ func TestParserErrorUnmarshal (t * testing.T ) {
1149
1149
var v struct {
1150
1150
A , B int
1151
1151
}
1152
1152
data := "a: 1\n =\n b: 2"
1153
1153
err := yaml .Unmarshal ([]byte (data ), & v )
1154
1154
asErr := new (yaml.ParserError )
1155
- if ! errors .As (err , & asErr ) {
1156
- t .Fatalf ("error returned by Unmarshal doesn't unwrap into yaml.ParserError" )
1157
- }
1155
+ assert .ErrorAs (t , err , & asErr )
1158
1156
expectedErr := & yaml.ParserError {
1159
1157
Message : "could not find expected ':'" ,
1160
1158
Line : 2 ,
1159
+ Column : 0 ,
1160
+ }
1161
+ assert .DeepEqual (t , expectedErr , asErr )
1162
+ }
1163
+
1164
+ func TestParserErrorDecoder (t * testing.T ) {
1165
+ var v any
1166
+ data := "value: -"
1167
+ err := yaml .NewDecoder (strings .NewReader (data )).Decode (& v )
1168
+ asErr := new (yaml.ParserError )
1169
+ assert .ErrorAs (t , err , & asErr )
1170
+ expectedErr := & yaml.ParserError {
1171
+ Message : "block sequence entries are not allowed in this context" ,
1172
+ Line : 0 ,
1173
+ Column : 7 ,
1161
1174
}
1162
1175
assert .DeepEqual (t , expectedErr , asErr )
1163
1176
}
Original file line number Diff line number Diff line change @@ -323,6 +323,7 @@ func failf(format string, args ...any) {
323
323
type ParserError struct {
324
324
Message string
325
325
Line int
326
+ Column int
326
327
}
327
328
328
329
func (e * ParserError ) Error () string {
You can’t perform that action at this time.
0 commit comments