From 0c0d4136136280c3b99175a3d3334ba41ffc2950 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Fri, 15 Apr 2022 10:24:03 +0530 Subject: [PATCH] update with only f functions Signed-off-by: Bisakh Mondal --- pkg/errors/errors.go | 41 +++++++++++++-------------------------- pkg/errors/errors_test.go | 39 ++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 0923ddf79cb..a74cbcea203 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -51,20 +51,13 @@ func (b *base) Format(s fmt.State, verb rune) { s.Write([]byte(b.Error())) } -// New returns an error that formats as the given text and a stacktrace with recent -// call frames. Each call to New returns a distinct error value even if the text is -// identical. An alternative to errors.New function. -func New(msg string) error { - return &base{ - info: msg, - stack: newStackTrace(), - err: nil, - } -} - -// Errorf formats according to a format specifier and returns a new error with -// a stacktrace containing recent call frames. An alternative to fmt.Errorf function. -func Errorf(format string, args ...interface{}) error { +// Newf formats according to a format specifier and returns a new error with a stacktrace +// with recent call frames. Each call to New returns a distinct error value even if the text is +// identical. An alternative of the errors.New function. +// +// If no args have been passed, it is same as `New` function without formatting. Character like +// '%' still has to be escaped in that scenario. +func Newf(format string, args ...interface{}) error { return &base{ info: fmt.Sprintf(format, args...), stack: newStackTrace(), @@ -72,22 +65,11 @@ func Errorf(format string, args ...interface{}) error { } } -// Wrap returns a new error with the supplied message as an argument and wrapping another error -// with a stacktrace containing recent call frames. -// -// If cause is nil, this is the same as New. -func Wrap(cause error, msg string) error { - return &base{ - info: msg, - stack: newStackTrace(), - err: cause, - } -} - // Wrapf returns a new error by formatting the error message with the supplied format specifier // and wrapping another error with a stacktrace containing recent call frames. // -// If cause is nil, this is the same as Errorf. +// If cause is nil, this is the same as fmt.Errorf. If no args have been passed, it is same as `Wrap` +// function without formatting. Character like '%' still has to be escaped in that scenario. func Wrapf(cause error, format string, args ...interface{}) error { return &base{ info: fmt.Sprintf(format, args...), @@ -133,6 +115,11 @@ func formatErrorChain(err error) string { return buf.String() } +// The functions `Is`, `As` & `Unwrap` provides a thin wrapper around the builtin errors +// package in go. Just for sake of completeness and correct autocompletion behaviors from +// IDEs they have been wrapped using functions instead of using variable to reference them +// as first class functions (eg: var Is = errros.Is ). + // Is is a wrapper of built-in errors.Is. It reports whether any error in err's // chain matches target. The chain consists of err itself followed by the sequence // of errors obtained by repeatedly calling Unwrap. diff --git a/pkg/errors/errors_test.go b/pkg/errors/errors_test.go index 934e18455d4..aa50712d833 100644 --- a/pkg/errors/errors_test.go +++ b/pkg/errors/errors_test.go @@ -17,41 +17,40 @@ import ( const msg = "test_error_message" const wrapper = "test_wrapper" -func TestNew(t *testing.T) { - err := New(msg) +func TestNewf(t *testing.T) { + err := Newf(msg) testutil.Equals(t, err.Error(), msg, "the root error message must match") - reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNew .*\/pkg\/errors\/errors_test\.go:\d+`) + reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewf .*\/pkg\/errors\/errors_test\.go:\d+`) testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.New") } -func TestErrorf(t *testing.T) { +func TestNewfFormatted(t *testing.T) { fmtMsg := msg + " key=%v" expectedMsg := msg + " key=value" - err := Errorf(fmtMsg, "value") + err := Newf(fmtMsg, "value") testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") - reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestErrorf .*\/pkg\/errors\/errors_test\.go:\d+`) + reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewfFormatted .*\/pkg\/errors\/errors_test\.go:\d+`) testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.New with format string") } -func TestWrap(t *testing.T) { - err := New(msg) - err = Wrap(err, wrapper) +func TestWrapf(t *testing.T) { + err := Newf(msg) + err = Wrapf(err, wrapper) expectedMsg := wrapper + ": " + msg testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") - reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/pkg\/errors\/errors_test\.go:\d+ -[[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/pkg\/errors\/errors_test\.go:\d+`) + reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrapf .*\/pkg\/errors\/errors_test\.go:\d+ +[[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrapf .*\/pkg\/errors\/errors_test\.go:\d+`) - t.Logf("%+v", err) testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.Wrap") } func TestUnwrap(t *testing.T) { // test with base error - err := New(msg) + err := Newf(msg) for i, tc := range []struct { err error @@ -64,11 +63,11 @@ func TestUnwrap(t *testing.T) { isNil: true, }, { - err: Wrap(err, wrapper), + err: Wrapf(err, wrapper), expected: "test_error_message", }, { - err: Wrap(Wrap(err, wrapper), wrapper), + err: Wrapf(Wrapf(err, wrapper), wrapper), expected: "test_wrapper: test_error_message", }, // check primitives errors @@ -77,7 +76,7 @@ func TestUnwrap(t *testing.T) { isNil: true, }, { - err: Wrap(stderrors.New("std-error"), wrapper), + err: Wrapf(stderrors.New("std-error"), wrapper), expected: "std-error", }, { @@ -98,7 +97,7 @@ func TestUnwrap(t *testing.T) { func TestCause(t *testing.T) { // test with base error that implements interface containing Unwrap method - err := New(msg) + err := Newf(msg) for i, tc := range []struct { err error @@ -111,11 +110,11 @@ func TestCause(t *testing.T) { isNil: true, }, { - err: Wrap(err, wrapper), + err: Wrapf(err, wrapper), isNil: true, }, { - err: Wrap(Wrap(err, wrapper), wrapper), + err: Wrapf(Wrapf(err, wrapper), wrapper), isNil: true, }, // check primitives errors @@ -124,7 +123,7 @@ func TestCause(t *testing.T) { expected: "std-error", }, { - err: Wrap(stderrors.New("std-error"), wrapper), + err: Wrapf(stderrors.New("std-error"), wrapper), expected: "std-error", }, {