Skip to content

Commit

Permalink
No longer removed interface{} in favor of 'any'
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Feb 13, 2023
1 parent 09c5e40 commit f874980
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
4 changes: 2 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (e *ErrTest) Is(target error) bool {

type ErrHasFields struct {
M string
F map[string]interface{}
F map[string]any
}

func (e *ErrHasFields) Error() string {
Expand All @@ -36,7 +36,7 @@ func (e *ErrHasFields) Is(target error) bool {
return ok
}

func (e *ErrHasFields) Fields() map[string]interface{} {
func (e *ErrHasFields) HasFields() map[string]any {
return e.F
}

Expand Down
26 changes: 13 additions & 13 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// HasFields Implement this interface to pass along unstructured context to the logger.
// It is the responsibility of Fields() implementation to unwrap the error chain and
// collect all errors that have `Fields()` defined.
// collect all errors that have `HasFields()` defined.
type HasFields interface {
Fields() map[string]interface{}
HasFields() map[string]any
}

// HasFormat True if the interface has the format method (from fmt package)
Expand All @@ -23,12 +23,12 @@ type HasFormat interface {
}

// WithFields Creates errors that conform to the `HasFields` interface
type WithFields map[string]interface{}
type WithFields map[string]any

// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is call, and the format specifier.
// If err is nil, Wrapf returns nil.
func (f WithFields) Wrapf(err error, format string, args ...interface{}) error {
func (f WithFields) Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func (f WithFields) Error(msg string) error {
}
}

func (f WithFields) Errorf(format string, args ...interface{}) error {
func (f WithFields) Errorf(format string, args ...any) error {
return &withFields{
stack: callstack.New(1),
fields: f,
Expand Down Expand Up @@ -117,16 +117,16 @@ func (c *withFields) StackTrace() callstack.StackTrace {
return c.stack.StackTrace()
}

func (c *withFields) Fields() map[string]interface{} {
result := make(map[string]interface{}, len(c.fields))
func (c *withFields) HasFields() map[string]any {
result := make(map[string]any, len(c.fields))
for key, value := range c.fields {
result[key] = value
}

// child fields have precedence as they are closer to the cause
var f HasFields
if errors.As(c.wrapped, &f) {
child := f.Fields()
child := f.HasFields()
if child == nil {
return result
}
Expand Down Expand Up @@ -169,10 +169,10 @@ func (c *withFields) FormatFields() string {
return buf.String()
}

// ToMap Returns the fields for the underlying error as map[string]interface{}
// ToMap Returns the fields for the underlying error as map[string]any
// If no fields are available returns nil
func ToMap(err error) map[string]interface{} {
result := map[string]interface{}{
func ToMap(err error) map[string]any {
result := map[string]any{
"excValue": err.Error(),
"excType": fmt.Sprintf("%T", Unwrap(err)),
}
Expand All @@ -190,7 +190,7 @@ func ToMap(err error) map[string]interface{} {
// Search the error chain for fields
var f HasFields
if errors.As(err, &f) {
for key, value := range f.Fields() {
for key, value := range f.HasFields() {
result[key] = value
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func ToLogrus(err error) logrus.Fields {
// Search the error chain for fields
var f HasFields
if errors.As(err, &f) {
for key, value := range f.Fields() {
for key, value := range f.HasFields() {
result[key] = value
}
}
Expand Down
2 changes: 1 addition & 1 deletion fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestWithFieldsErrorValue(t *testing.T) {
}

func TestHasFields(t *testing.T) {
hf := &ErrHasFields{M: "error", F: map[string]interface{}{"file": "errors.go"}}
hf := &ErrHasFields{M: "error", F: map[string]any{"file": "errors.go"}}
err := errors.WithFields{"key1": "value1"}.Wrap(hf, "")
m := errors.ToMap(err)
require.NotNil(t, m)
Expand Down
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ module github.com/mailgun/errors
go 1.19

require (
github.com/ahmetb/go-linq v3.0.0+incompatible // indirect
github.com/ahmetb/go-linq v3.0.0+incompatible
github.com/mailgun/holster/v4 v4.11.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ github.com/ahmetb/go-linq v3.0.0+incompatible/go.mod h1:PFffvbdbtw+QTB0WKRP0cNht
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/mailgun/holster/v4 v4.11.0 h1:c9DT3QZCfiUgcmOYvxJI9rRhLbtX6IqfaClrMjLxkLE=
github.com/mailgun/holster/v4 v4.11.0/go.mod h1:sXpF+rzEqA89uBEiX19FrVUdbCUKDfR4GRdXmIkINQQ=
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/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
Expand All @@ -15,8 +19,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
6 changes: 3 additions & 3 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func (w *withStack) Is(target error) bool {
return ok
}

func (w *withStack) Fields() map[string]interface{} {
func (w *withStack) HasFields() map[string]any {
if child, ok := w.error.(HasFields); ok {
return child.Fields()
return child.HasFields()
}

var f HasFields
if errors.As(w.error, &f) {
return f.Fields()
return f.HasFields()
}

return nil
Expand Down

0 comments on commit f874980

Please sign in to comment.