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

Do not use errgo nor pkg/errors for root errors, matches stdlib #584

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions errors/errctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,41 @@ func (err ErrCtx) Unwrap() error {
return err.err
}

func New(ctx context.Context, message string) error {
return ErrCtx{ctx: ctx, err: errgo.New(message)}
// New wraps errors.New from the pkg/errors library
//
// These errors are usually created outside any function code at the top of
// files, so no context is needed.
Comment on lines +28 to +31
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: I would keep a first line of comment explaining what this function do.

Suggested change
// New wraps errors.New from the pkg/errors library
//
// These errors are usually created outside any function code at the top of
// files, so no context is needed.
// New returns an error that formats as the given text.
// New wraps errors.New from the pkg/errors library
//
// These errors are usually created outside any function code at the top of
// files, so no context is needed.

func New(message string) error {
EtienneM marked this conversation as resolved.
Show resolved Hide resolved
return errors.New(message)
}

func NewWithCtx(ctx context.Context, message string) error {
return ErrCtx{ctx: ctx, err: errors.New(message)}
}

func Newf(ctx context.Context, format string, args ...interface{}) error {
return ErrCtx{ctx: ctx, err: errgo.Newf(format, args...)}
return ErrCtx{ctx: ctx, err: errors.Errorf(format, args...)}
}

func Errorf(ctx context.Context, format string, args ...interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need both Newf and Errorf? correct me if I'm wrong but they seem identical.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right it was to prevent breaking the API more

Copy link
Member

Choose a reason for hiding this comment

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

Hence should we mark one of these two methods as deprecated?

return ErrCtx{ctx: ctx, err: errors.Errorf(format, args...)}
}

// Notef is wrapping an error with the underneath errgo library
func Notef(ctx context.Context, err error, format string, args ...interface{}) error {
return ErrCtx{ctx: ctx, err: errgo.Notef(err, format, args...)}
}

// Wrap is wrapping an error with the underneath errors library
func Wrap(ctx context.Context, err error, message string) error {
return ErrCtx{ctx: ctx, err: errors.Wrap(err, message)}
}

// Wrapf is wrapping an error with the underneath errors library
func Wrapf(ctx context.Context, err error, format string, args ...interface{}) error {
return ErrCtx{ctx: ctx, err: errors.Wrapf(err, format, args...)}
}

func Errorf(ctx context.Context, format string, args ...interface{}) error {
return ErrCtx{ctx: ctx, err: errors.Errorf(format, args...)}
}

// RootCtxOrFallback unwrap all wrapped errors from err to get the deepest context
// from ErrCtx errors. If there is no wrapped ErrCtx RootCtxOrFallback returns ctx from parameter.
func RootCtxOrFallback(ctx context.Context, err error) context.Context {
Expand Down