Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant dependencies #9

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"

"github.com/mailgun/errors/callstack"
"github.com/sirupsen/logrus"
)

// HasFields Implement this interface to pass along unstructured context to the logger.
Expand Down Expand Up @@ -204,6 +203,10 @@ func (c *fields) FormatFields() string {
// 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]any {
if err == nil {
return nil
}

result := map[string]any{
"excValue": err.Error(),
"excType": fmt.Sprintf("%T", Unwrap(err)),
Expand All @@ -229,32 +232,10 @@ func ToMap(err error) map[string]any {
return result
}

// ToLogrus Returns the context and stacktrace information for the underlying error as logrus.Fields{}
// returns empty logrus.Fields{} if err has no context or no stacktrace
// ToLogrus Returns the context and stacktrace information for the underlying error
// that could be used as logrus.Fields
//
// logrus.Fields(errors.ToLogrus(err)).WithField("tid", 1).Error(err)
func ToLogrus(err error) logrus.Fields {
result := logrus.Fields{
"excValue": err.Error(),
"excType": fmt.Sprintf("%T", Unwrap(err)),
}

// Find any errors with StackTrace information if available
var stack callstack.HasStackTrace
if Last(err, &stack) {
trace := stack.StackTrace()
caller := callstack.GetLastFrame(trace)
result["excFuncName"] = caller.Func
result["excLineNum"] = caller.LineNo
result["excFileName"] = caller.File
}

// Search the error chain for fields
var f HasFields
if errors.As(err, &f) {
for key, value := range f.HasFields() {
result[key] = value
}
}
return result
func ToLogrus(err error) map[string]any {
return ToMap(err)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/mailgun/errors
go 1.21

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

require (
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/ahmetb/go-linq v3.0.0+incompatible h1:qQkjjOXKrKOTy83X8OpRmnKflXKQIL/mC/gMVVDMhOA=
github.com/ahmetb/go-linq v3.0.0+incompatible/go.mod h1:PFffvbdbtw+QTB0WKRP0cNht7vnCfnGlEpak/DVg5cY=
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=
Expand All @@ -8,13 +6,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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=
Expand Down
9 changes: 4 additions & 5 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"testing"

"github.com/ahmetb/go-linq"
"github.com/mailgun/errors"
"github.com/mailgun/errors/callstack"
"github.com/stretchr/testify/assert"
Expand All @@ -32,9 +31,9 @@ func TestWrapWithFieldsAndStack(t *testing.T) {

trace := stack.StackTrace()
caller := callstack.GetLastFrame(trace)
assert.Contains(t, fmt.Sprintf("%+v", stack), "errors/stack_test.go:18")
assert.Contains(t, fmt.Sprintf("%+v", stack), "errors/stack_test.go:17")
assert.Equal(t, "errors_test.TestWrapWithFieldsAndStack", caller.Func)
assert.Equal(t, 18, caller.LineNo)
assert.Equal(t, 17, caller.LineNo)
Comment on lines -35 to +36
Copy link
Contributor Author

@vtopc vtopc Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed one line, so this should be shifted.

}

func TestStack(t *testing.T) {
Expand All @@ -48,8 +47,8 @@ func TestStack(t *testing.T) {
funcs = append(funcs, fmt.Sprintf("%n", frame))
}
}
assert.True(t, linq.From(files).Contains("stack_test.go"))
assert.True(t, linq.From(funcs).Contains("TestStack"), funcs)
assert.Contains(t, files, "stack_test.go")
assert.Contains(t, funcs, "TestStack")
}

func TestStackWrapped(t *testing.T) {
Expand Down
Loading