Skip to content

Commit

Permalink
test(errArray): Broken ArrayEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Sep 2, 2023
1 parent cb09f36 commit 4f6b4b8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,39 @@ func TestErrorsArraysHandleRichErrors(t *testing.T) {
require.True(t, ok, "Expected serialized error to be a map, got %T.", serialized)
assert.Equal(t, "egad", errMap["error"], "Unexpected standard error string.")
}

func TestErrArrayBrokenEncoder(t *testing.T) {
t.Parallel()

failWith := errors.New("great sadness")
err := (brokenArrayObjectEncoder{
Err: failWith,
ObjectEncoder: zapcore.NewMapObjectEncoder(),
}).AddArray("errors", errArray{
errors.New("foo"),
errors.New("bar"),
})
require.Error(t, err, "Expected error from broken encoder.")
assert.ErrorIs(t, err, failWith, "Unexpected error.")
}

// brokenArrayObjectEncoder is an ObjectEncoder
// that builds a broken ArrayEncoder.
type brokenArrayObjectEncoder struct {
zapcore.ObjectEncoder
zapcore.ArrayEncoder

Err error // error to return
}

func (enc brokenArrayObjectEncoder) AddArray(key string, marshaler zapcore.ArrayMarshaler) error {
return enc.ObjectEncoder.AddArray(key,
zapcore.ArrayMarshalerFunc(func(ae zapcore.ArrayEncoder) error {
enc.ArrayEncoder = ae
return marshaler.MarshalLogArray(enc)
}))
}

func (enc brokenArrayObjectEncoder) AppendObject(zapcore.ObjectMarshaler) error {
return enc.Err
}

0 comments on commit 4f6b4b8

Please sign in to comment.