Skip to content

Commit f0f6bb8

Browse files
Merge pull request #269 from newrelic/v3__add_invalid_json_line_to_error
add invalid json line content to error
2 parents c18a111 + 56e36c8 commit f0f6bb8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

jmx/jmx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func receiveResult(lineC chan []byte, queryErrC chan error, cancelFn context.Can
360360
}
361361
var r map[string]interface{}
362362
if err = json.Unmarshal(line, &r); err != nil {
363-
err = fmt.Errorf("invalid return value for query: %s, error: %s", objectPattern, err)
363+
err = fmt.Errorf("invalid return value for query: %s, error: %s, line: %s", objectPattern, err, line)
364364
return
365365
}
366366
if result == nil {

jmx/jmx_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"errors"
78
"flag"
89
"fmt"
910
"os"
@@ -209,6 +210,25 @@ func Test_receiveResult_warningsDoNotBreakResultReception(t *testing.T) {
209210
assert.Equal(t, fmt.Sprintf("[WARN] %s\n", warningMessage), buf.String())
210211
}
211212

213+
func Test_receiveResult_invalidJsonIsPrintedInError(t *testing.T) {
214+
215+
var buf bytes.Buffer
216+
log.SetOutput(&buf)
217+
218+
_, cancelFn := context.WithCancel(context.Background())
219+
220+
resultCh := make(chan []byte, 2)
221+
queryErrCh := make(chan error)
222+
outTimeout := time.Duration(timeoutMillis) * time.Millisecond
223+
224+
resultCh <- []byte("#this is an invalid json")
225+
226+
result, err := receiveResult(resultCh, queryErrCh, cancelFn, "foo", outTimeout)
227+
228+
assert.Equal(t, err, errors.New("invalid return value for query: foo, error: invalid character '#' looking for beginning of value, line: #this is an invalid json"))
229+
assert.Nil(t, result)
230+
}
231+
212232
func Test_DefaultPath_IsCorrectForOs(t *testing.T) {
213233
os := runtime.GOOS
214234
switch os {

0 commit comments

Comments
 (0)