Skip to content

Commit

Permalink
Merge pull request influxdata#1577 from influxdata/md-nil-error
Browse files Browse the repository at this point in the history
Fix panic on nil error field
  • Loading branch information
desa authored Sep 20, 2017
2 parents 5d228b2 + a24968a commit e5a3d7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion services/diagnostic/internal/log/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,12 @@ func (s ErrorField) WriteTo(w *bufio.Writer) (n int64, err error) {
return
}

m, err = writeString(w, s.err.Error())
errStr := "nil"
if s.err != nil {
errStr = s.err.Error()
}

m, err = writeString(w, errStr)
n += int64(m)
if err != nil {
return
Expand Down
9 changes: 9 additions & 0 deletions services/diagnostic/internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ func TestLoggerWithoutContext(t *testing.T) {
log.Error(errors.New("this")),
},
},
{
name: "nil error field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test err=nil\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Error(nil),
},
},
{
name: "complex error field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test err=\"this is \\\" a test/yeah\"\n", nowStr),
Expand Down

0 comments on commit e5a3d7f

Please sign in to comment.