Skip to content

Badly coded MarshalLogObject can panic zap #1504

@alshopov

Description

@alshopov

Describe the bug
A panic in implementer of zapcore.ObjectMarshaler (i.e. badly coded MarshalLogObject(ObjectEncoder) error) causes zap to panic which causes the app using it to panic

To Reproduce
Check this playgound: https://go.dev/play/p/HqAKQ6nGxgy

Here is the important part:

type user struct{}

func (u user) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	panic("Catch me if you can")
}

Expected behavior
zap should not panic but make a good effort to signal this problem

Additional context
Check similar cases that are guarded against panics in (encodeStringer](https://github.com/uber-go/zap/blob/master/zapcore/field.go#L214)
Also see discussion in this thread: #1501 (comment)


This problem also shows in zap.Object, zap.Inline, zap.Array, zap.Dict (if one of the fields has the problem) - basically any field for which we call a method on the logged. This is similar to the workaround for Stringer. Note that there are similar cases for zap.Error when not all cases are covered - for example a panic with an object that causes a panic itself
when being converted to a string

As a minimum we need some coverage for all these cases or refactoring in a way that shows that we reuse enough of the implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions