Skip to content

Commit

Permalink
${key}Verbose and ${key}Causes for wrapped errors
Browse files Browse the repository at this point in the history
encodeError cannot provide ${key}Verbose and ${key}Causes fields
if the error implements fmt.Formatter and/or errorGroup interfaces
but is wrapped.

This change uses errors.As instead of a type switch to check if
the given error and the underlying errors are compatible with the
above interfaces, and computes ${key}Verbose and ${key}Causes fields
with unwrapped error.

This commit also upgrades github.com/pkg/errors to v0.9.1 because
v0.8.1 doesn't support Unwrap method.
  • Loading branch information
jkawamoto committed May 18, 2021
1 parent debd2f1 commit ba81f32
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 2 additions & 0 deletions benchmarks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module go.uber.org/zap
go 1.13

require (
github.com/pkg/errors v0.8.1
github.com/pkg/errors v0.9.1
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
go.uber.org/atomic v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
13 changes: 8 additions & 5 deletions zapcore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package zapcore

import (
"errors"
"fmt"
"reflect"
"sync"
Expand Down Expand Up @@ -63,11 +64,13 @@ func encodeError(key string, err error, enc ObjectEncoder) (retErr error) {
basic := err.Error()
enc.AddString(key, basic)

switch e := err.(type) {
case errorGroup:
return enc.AddArray(key+"Causes", errArray(e.Errors()))
case fmt.Formatter:
verbose := fmt.Sprintf("%+v", e)
var eg errorGroup
if errors.As(err, &eg) {
return enc.AddArray(key+"Causes", errArray(eg.Errors()))
}
var f fmt.Formatter
if errors.As(err, &f) {
verbose := fmt.Sprintf("%+v", f)
if verbose != basic {
// This is a rich error type, like those produced by
// github.com/pkg/errors.
Expand Down
48 changes: 44 additions & 4 deletions zapcore/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ func TestErrorEncoding(t *testing.T) {
},
},
},
{
k: "err",
iface: fmt.Errorf("wrap: %w", multierr.Combine(
errors.New("foo"),
errors.New("bar"),
errors.New("baz"),
)),
want: map[string]interface{}{
"err": "wrap: foo; bar; baz",
"errCauses": []interface{}{
map[string]interface{}{"error": "foo"},
map[string]interface{}{"error": "bar"},
map[string]interface{}{"error": "baz"},
},
},
},
{
k: "err",
iface: richErrors.WithMessage(multierr.Combine(
errors.New("foo"),
errors.New("bar"),
errors.New("baz"),
), "wrap"),
want: map[string]interface{}{
"err": "wrap: foo; bar; baz",
"errCauses": []interface{}{
map[string]interface{}{"error": "foo"},
map[string]interface{}{"error": "bar"},
map[string]interface{}{"error": "baz"},
},
},
},
{
k: "e",
iface: customMultierr{},
Expand All @@ -119,6 +151,14 @@ func TestErrorEncoding(t *testing.T) {
"kVerbose": "egad\nfailed",
},
},
{
k: "k",
iface: fmt.Errorf("wrap: %w", richErrors.WithMessage(errors.New("egad"), "failed")),
want: map[string]interface{}{
"k": "wrap: failed: egad",
"kVerbose": "egad\nfailed",
},
},
{
k: "error",
iface: multierr.Combine(
Expand All @@ -134,10 +174,10 @@ func TestErrorEncoding(t *testing.T) {
"errorCauses": []interface{}{
map[string]interface{}{
"error": "hello: foo; bar",
"errorVerbose": "the following errors occurred:\n" +
" - foo\n" +
" - bar\n" +
"hello",
"errorCauses": []interface{}{
map[string]interface{}{"error": "foo"},
map[string]interface{}{"error": "bar"},
},
},
map[string]interface{}{"error": "baz"},
map[string]interface{}{"error": "world: qux", "errorVerbose": "qux\nworld"},
Expand Down
2 changes: 2 additions & 0 deletions zapgrpc/internal/test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down

0 comments on commit ba81f32

Please # to comment.