Skip to content

Commit

Permalink
Support nesting of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv committed Apr 12, 2023
1 parent a99527f commit 8ce30df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
24 changes: 20 additions & 4 deletions fields_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package zap

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -30,20 +31,35 @@ import (

func TestErrorWithFields(t *testing.T) {
rootErr := errors.New("root err")
wrapped := WrapError(rootErr,
wrap1 := fmt.Errorf("wrap1: %w", rootErr)
wrap2 := WrapError(wrap1,
String("user", "foo"),
Int("count", 12),
)

assert.True(t, errors.Is(wrapped, rootErr), "errors.Is")
assert.True(t, errors.Is(wrap2, rootErr), "errors.Is")
assert.True(t, errors.Is(wrap2, wrap1), "errors.Is")

enc := zapcore.NewMapObjectEncoder()
Error(wrapped).AddTo(enc)
Error(wrap2).AddTo(enc)
assert.Equal(t, map[string]any{
"error": rootErr.Error(),
"error": "wrap1: root err",
"errorFields": map[string]any{
"user": "foo",
"count": int64(12),
},
}, enc.Fields)

wrap3 := fmt.Errorf("wrap3: %w", wrap2)
wrap4 := WrapError(wrap3, Bool("wrap4", true))

Error(wrap4).AddTo(enc)
assert.Equal(t, map[string]any{
"error": "wrap3: wrap1: root err",
"errorFields": map[string]any{
"user": "foo",
"count": int64(12),
"wrap4": true,
},
}, enc.Fields)
}
31 changes: 29 additions & 2 deletions zapcore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package zapcore

import (
"errors"
"fmt"
"reflect"

"go.uber.org/multierr"
"go.uber.org/zap/internal/pool"
)

Expand Down Expand Up @@ -76,15 +78,40 @@ func encodeError(key string, err error, enc ObjectEncoder) (retErr error) {
}
}

if errObj, ok := err.(ObjectMarshaler); ok {
if err := enc.AddObject(key+"Fields", errObj); err != nil {
// Unwrap the error and marshal any ObjectMarshalers in the error chain.

marshalers := make(mergedMarshalers, 0, 1)
for e := err; e != nil; e = errors.Unwrap(e) {
errObj, ok := e.(ObjectMarshaler)
if ok {
marshalers = append(marshalers, errObj)
}
}

switch {
case len(marshalers) == 1:
if err := enc.AddObject(key+"Fields", marshalers[0]); err != nil {
return err
}

Check warning on line 95 in zapcore/error.go

View check run for this annotation

Codecov / codecov/patch

zapcore/error.go#L94-L95

Added lines #L94 - L95 were not covered by tests
case len(marshalers) > 1:
if err := enc.AddObject(key+"Fields", marshalers); err != nil {
return err
}

Check warning on line 99 in zapcore/error.go

View check run for this annotation

Codecov / codecov/patch

zapcore/error.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}

return nil
}

type mergedMarshalers []ObjectMarshaler

func (ms mergedMarshalers) MarshalLogObject(enc ObjectEncoder) error {
var errs error
for _, m := range ms {
errs = multierr.Append(errs, m.MarshalLogObject(enc))
}
return errs
}

type errorGroup interface {
// Provides read-only access to the underlying list of errors, preferably
// without causing any allocs.
Expand Down

0 comments on commit 8ce30df

Please sign in to comment.