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 stack errorf function #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 9 additions & 3 deletions stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,28 @@ func ErrStack() error {
return callers(3)
}

// Errorf formats an error using fmt.Errorf and joins it with
// a stack if the error does not already have one.
func Errorf(format string, a ...any) error {
return joinErrors(nil, fmt.Errorf(format, a...))
}
Comment on lines +205 to +207

Choose a reason for hiding this comment

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

Can we get this put into the main errdefs package as something like errdefs.New? Or something like errdefs.Errorf. I think this is the functionality we want, but I'd like it if it were easier to access and didn't require using a subpackage especially since stack is a bit of a generic name.

Copy link
Member

Choose a reason for hiding this comment

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

ping @dmcgowan


// Join adds a stack if there is no stack included to the errors
// and returns a joined error with the stack hidden from the error
// output. The stack error shows up when Unwrapped or formatted
// with `%+v`.
func Join(errs ...error) error {
return joinErrors(nil, errs)
return joinErrors(nil, errs...)
}

// WithStack will check if the error already has a stack otherwise
// return a new error with the error joined with a stack error
// Any helpers will be skipped.
func WithStack(ctx context.Context, errs ...error) error {
return joinErrors(ctx.Value(helperKey{}), errs)
return joinErrors(ctx.Value(helperKey{}), errs...)
}

func joinErrors(helperVal any, errs []error) error {
func joinErrors(helperVal any, errs ...error) error {
var filtered []error
var collapsible []error
var hasStack bool
Expand Down
Loading