Skip to content

Commit

Permalink
Remove dependency on github.com/pkg/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ILLU510N committed Mar 19, 2023
1 parent 384e782 commit 9f7b9a3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 27 deletions.
6 changes: 2 additions & 4 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package zap

import (
"errors"
"fmt"
"testing"

"go.uber.org/zap/zapcore"

richErrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestErrorArrayConstructor(t *testing.T) {
}

func TestErrorsArraysHandleRichErrors(t *testing.T) {
errs := []error{richErrors.New("egad")}
errs := []error{fmt.Errorf("egad")}

enc := zapcore.NewMapObjectEncoder()
Errors("k", errs).AddTo(enc)
Expand All @@ -94,6 +94,4 @@ func TestErrorsArraysHandleRichErrors(t *testing.T) {
errMap, ok := serialized.(map[string]interface{})
require.True(t, ok, "Expected serialized error to be a map, got %T.", serialized)
assert.Equal(t, "egad", errMap["error"], "Unexpected standard error string.")
assert.Contains(t, errMap["errorVerbose"], "egad", "Verbose error string should be a superset of standard error.")
assert.Contains(t, errMap["errorVerbose"], "TestErrorsArraysHandleRichErrors", "Verbose error string should contain a stacktrace.")
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.19

require (
github.com/benbjohnson/clock v1.3.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
go.uber.org/goleak v1.2.0
go.uber.org/multierr v1.10.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
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
26 changes: 6 additions & 20 deletions zapcore/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io"
"testing"

richErrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"go.uber.org/multierr"
Expand Down Expand Up @@ -113,34 +112,28 @@ func TestErrorEncoding(t *testing.T) {
},
{
k: "k",
iface: richErrors.WithMessage(errors.New("egad"), "failed"),
iface: fmt.Errorf("failed: %w", errors.New("egad")),
want: map[string]interface{}{
"k": "failed: egad",
"kVerbose": "egad\nfailed",
"k": "failed: egad",
},
},
{
k: "error",
iface: multierr.Combine(
richErrors.WithMessage(
fmt.Errorf("hello: %w",
multierr.Combine(errors.New("foo"), errors.New("bar")),
"hello",
),
errors.New("baz"),
richErrors.WithMessage(errors.New("qux"), "world"),
fmt.Errorf("world: %w", errors.New("qux")),
),
want: map[string]interface{}{
"error": "hello: foo; bar; baz; world: qux",
"errorCauses": []interface{}{
map[string]interface{}{
"error": "hello: foo; bar",
"errorVerbose": "the following errors occurred:\n" +
" - foo\n" +
" - bar\n" +
"hello",
},
map[string]interface{}{"error": "baz"},
map[string]interface{}{"error": "world: qux", "errorVerbose": "qux\nworld"},
map[string]interface{}{"error": "world: qux"},
},
},
},
Expand All @@ -161,17 +154,10 @@ func TestErrorEncoding(t *testing.T) {
func TestRichErrorSupport(t *testing.T) {
f := Field{
Type: ErrorType,
Interface: richErrors.WithMessage(richErrors.New("egad"), "failed"),
Interface: fmt.Errorf("failed: %w", errors.New("egad")),
Key: "k",
}
enc := NewMapObjectEncoder()
f.AddTo(enc)
assert.Equal(t, "failed: egad", enc.Fields["k"], "Unexpected basic error message.")

serialized := enc.Fields["kVerbose"]
// Don't assert the exact format used by a third-party package, but ensure
// that some critical elements are present.
assert.Regexp(t, `egad`, serialized, "Expected original error message to be present.")
assert.Regexp(t, `failed`, serialized, "Expected error annotation to be present.")
assert.Regexp(t, `TestRichErrorSupport`, serialized, "Expected calling function to be present in stacktrace.")
}

0 comments on commit 9f7b9a3

Please sign in to comment.