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

Add slog compatibility, move to Go 1.21 #97

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion errtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func wrap(err error, callerPC uintptr) error {
}

// Format writes the return trace for given error to the writer.
// The output takes a fromat similar to the following:
// The output takes a format similar to the following:
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks

//
// <error message>
//
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module braces.dev/errtrace

go 1.20
go 1.21
34 changes: 34 additions & 0 deletions log_go121.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build go1.21

package errtrace

import (
"bytes"
"log/slog"
)

// LogValue implements the [slog.LogValuer] interface.
func (e *errTrace) LogValue() slog.Value {
var outb bytes.Buffer
err := writeTree(&outb, buildTraceTree(e))
if err != nil {
return slog.GroupValue(
slog.String("message", e.Error()),
slog.Any("formatErr", err),
)

Check warning on line 18 in log_go121.go

View check run for this annotation

Codecov / codecov/patch

log_go121.go#L11-L18

Added lines #L11 - L18 were not covered by tests
}

return slog.StringValue(outb.String())

Check warning on line 21 in log_go121.go

View check run for this annotation

Codecov / codecov/patch

log_go121.go#L21

Added line #L21 was not covered by tests
}

// LogAttr builds a slog attribute for an error.
// It will log the error with an error trace
// if the error has been wrapped with this package.
// Otherwise, the error message will be logged as is.
//
// Usage:
//
// slog.Default().Error("msg here", errtrace.LogAttr(err))
func LogAttr(err error) slog.Attr {
return slog.Any("error", err)

Check warning on line 33 in log_go121.go

View check run for this annotation

Codecov / codecov/patch

log_go121.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}
Loading