From 7664a77fed6317ab8b2c765125daafdba65b2193 Mon Sep 17 00:00:00 2001 From: Soulou Date: Fri, 7 Apr 2023 00:53:57 +0200 Subject: [PATCH] Do not use errgo nor pkg/errors for root errors Related to #582 --- errors/errctx.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/errors/errctx.go b/errors/errctx.go index 9d014aa22..5aed0d731 100644 --- a/errors/errctx.go +++ b/errors/errctx.go @@ -2,6 +2,8 @@ package errors import ( "context" + stderrors "errors" + "fmt" "github.com/pkg/errors" "gopkg.in/errgo.v1" @@ -25,30 +27,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 standard library +// +// These errors are usually created outside any function code at the top of +// files, so no context is needed nor wrapping is needed. +func New(message string) error { + return stderrors.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: fmt.Errorf(format, args...)} +} + +func Errorf(ctx context.Context, format string, args ...interface{}) error { + return Newf(ctx, 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 errgo 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 errgo 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 {