| 212 | } |
| 213 | |
| 214 | func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) { |
| 215 | // Try to capture panics (from nil references or otherwise) when calling |
| 216 | // the String() method, similar to https://golang.org/src/fmt/print.go#L540 |
| 217 | defer func() { |
| 218 | if err := recover(); err != nil { |
| 219 | // If it's a nil pointer, just say "<nil>". The likeliest causes are a |
| 220 | // Stringer that fails to guard against nil or a nil pointer for a |
| 221 | // value receiver, and in either case, "<nil>" is a nice result. |
| 222 | if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() { |
| 223 | enc.AddString(key, "<nil>") |
| 224 | return |
| 225 | } |
| 226 | |
| 227 | retErr = fmt.Errorf("PANIC=%v", err) |
| 228 | } |
| 229 | }() |
| 230 | |
| 231 | enc.AddString(key, stringer.(fmt.Stringer).String()) |
| 232 | return nil |
| 233 | } |