Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
log: avoid panic marshaling nil error (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
voutasaurus authored and bensigelman committed Dec 31, 2016
1 parent ac5446f commit 5e5abf8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion log/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ func (lf Field) Marshal(visitor Encoder) {
case float64Type:
visitor.EmitFloat64(lf.key, math.Float64frombits(uint64(lf.numericVal)))
case errorType:
visitor.EmitString(lf.key, lf.interfaceVal.(error).Error())
if err, ok := lf.interfaceVal.(error); ok {
visitor.EmitString(lf.key, err.Error())
} else {
visitor.EmitString(lf.key, "<nil>")
}
case objectType:
visitor.EmitObject(lf.key, lf.interfaceVal)
case lazyLoggerType:
Expand Down
4 changes: 4 additions & 0 deletions log/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func TestFieldString(t *testing.T) {
field: Error(fmt.Errorf("err msg")),
expected: "error:err msg",
},
{
field: Error(nil),
expected: "error:<nil>",
},
}
for i, tc := range testCases {
if str := tc.field.String(); str != tc.expected {
Expand Down

0 comments on commit 5e5abf8

Please sign in to comment.