Skip to content

Commit

Permalink
Merge pull request #1 from abyssparanoia/feature/impl-zap-marshal-lag…
Browse files Browse the repository at this point in the history
…-object

feat: impl zap object marshaler
  • Loading branch information
abyssparanoia authored Nov 19, 2024
2 parents 2e5ed7f + db2ed9d commit 06f4124
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
45 changes: 45 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"

"github.com/google/uuid"
"go.uber.org/zap/zapcore"
)

// New creates a new error with message
Expand Down Expand Up @@ -262,3 +263,47 @@ func (x *Error) LogValue() slog.Value {

return slog.GroupValue(attrs...)
}

func (x *Error) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if x == nil {
enc.AddString("message", "<nil>")
return nil
}
enc.AddString("message", x.msg)
enc.AddArray("values", zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error {
for k, v := range x.values {
inner.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
enc.AddString("key", k)
enc.AddReflected("value", v)
return nil
}))
}
return nil
}))
var traces []string
for _, st := range x.StackTrace() {
traces = append(traces, fmt.Sprintf("%s:%d %s", st.file(), st.line(), st.name()))
}
enc.AddArray("stacktrace", zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error {
for _, st := range traces {
inner.AppendString(st)
}
return nil
}))

if x.cause != nil {
got := false
if inCause := x.Unwrap(); inCause != nil {
if err, ok := inCause.(*Error); ok {
if err := err.MarshalLogObject(enc); err != nil {
return err
}
}
got = true
}
if !got {
enc.AddString("cause", x.cause.Error())
}
}
return nil
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/m-mizutani/goerr

go 1.21

require github.com/google/uuid v1.6.0
require (
github.com/google/uuid v1.6.0
go.uber.org/zap v1.27.0
)

require go.uber.org/multierr v1.10.0 // indirect
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 06f4124

Please sign in to comment.