Permalink
Browse files

log: avoid panic marshaling nil error (#131)

  • Loading branch information...
1 parent ac5446f commit 5e5abf838007b08f96ae057bc182636a178da0b9 @voutasaurus voutasaurus committed with bensigelman Dec 31, 2016
Showing with 9 additions and 1 deletion.
  1. +5 −1 log/field.go
  2. +4 −0 log/field_test.go
View
@@ -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:
View
@@ -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 {

0 comments on commit 5e5abf8

Please sign in to comment.