Skip to content

Commit

Permalink
make sure parse error includes offending text (influxdata#7561)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka authored and jaecktec committed Jun 8, 2020
1 parent 5a8fba7 commit f507a9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion plugins/parsers/influx/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ func (e *ParseError) Error() string {
buffer = buffer[:eol]
}
if len(buffer) > maxErrorBufferSize {
buffer = buffer[:maxErrorBufferSize] + "..."
startEllipsis := true
offset := e.Offset - e.LineOffset
start := offset - maxErrorBufferSize
if start < 0 {
startEllipsis = false
start = 0
}
// if we trimmed it the column won't line up. it'll always be the last character,
// because the parser doesn't continue past it, but point it out anyway so
// it's obvious where the issue is.
buffer = buffer[start:offset] + "<-- here"
if startEllipsis {
buffer = "..." + buffer
}
}
return fmt.Sprintf("metric parse error: %s at %d:%d: %q", e.msg, e.LineNumber, e.Column, buffer)
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/parsers/influx/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func TestParserErrorString(t *testing.T) {
{
name: "buffer too long",
input: []byte("cpu " + strings.Repeat("ab", maxErrorBufferSize) + "=invalid\ncpu value=42"),
errString: "metric parse error: expected field at 1:2054: \"cpu " + strings.Repeat("ab", maxErrorBufferSize)[:maxErrorBufferSize-4] + "...\"",
errString: "metric parse error: expected field at 1:2054: \"...b" + strings.Repeat("ab", maxErrorBufferSize/2-1) + "=<-- here\"",
},
{
name: "multiple line error",
Expand Down Expand Up @@ -834,7 +834,7 @@ func TestStreamParserErrorString(t *testing.T) {
name: "buffer too long",
input: []byte("cpu " + strings.Repeat("ab", maxErrorBufferSize) + "=invalid\ncpu value=42"),
errs: []string{
"metric parse error: expected field at 1:2054: \"cpu " + strings.Repeat("ab", maxErrorBufferSize)[:maxErrorBufferSize-4] + "...\"",
"metric parse error: expected field at 1:2054: \"...b" + strings.Repeat("ab", maxErrorBufferSize/2-1) + "=<-- here\"",
},
},
{
Expand Down

0 comments on commit f507a9f

Please sign in to comment.