Skip to content

Commit 3c605c1

Browse files
committed
show code snippet when only line number is known
column is zero in the case. this happens when the error came from go-yaml parser.
1 parent 6a75475 commit 3c605c1

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

error.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (e *Error) PrettyPrint(w io.Writer, source []byte) {
6868
bold.Fprint(w, e.Message)
6969
gray.Fprintf(w, " [%s]\n", e.Kind)
7070

71-
if len(source) == 0 || e.Line <= 0 || e.Column <= 0 {
71+
if len(source) == 0 || e.Line <= 0 {
7272
return
7373
}
7474
line, ok := e.getLine(source)
@@ -98,6 +98,10 @@ func (e *Error) getLine(source []byte) (string, bool) {
9898
}
9999

100100
func (e *Error) getIndicator(line string) string {
101+
if e.Column <= 0 {
102+
return ""
103+
}
104+
101105
start := e.Column - 1 // Column is 1-based
102106

103107
// Count width of non-space characters after '^' for underline

error_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ func TestErrorPrettyPrint(t *testing.T) {
177177
source: "aaa\nbbb\nccc",
178178
expected: "filename.txt:2:6: error at out of source [kind]",
179179
},
180+
{
181+
message: "error at zero column",
182+
line: 1,
183+
column: 0,
184+
source: "this is source",
185+
expected: `filename.txt:1:0: error at zero column [kind]
186+
|
187+
1 | this is source
188+
| `,
189+
},
190+
{
191+
message: "error at zero line and zero column",
192+
line: 0,
193+
column: 0,
194+
source: "this is source",
195+
expected: `filename.txt:0:0: error at zero line and zero column [kind]`,
196+
},
180197
}
181198

182199
for _, tc := range testCases {

testdata/examples/broken_yaml.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test.yaml:0:0: could not parse as YAML: yaml: line 6: mapping values are not allowed in this context [yaml-syntax]
1+
test.yaml:6:0: could not parse as YAML: yaml: line 6: mapping values are not allowed in this context [yaml-syntax]

0 commit comments

Comments
 (0)