From a065237ccc9bba548ef23d1c51cdd1d97ff1e918 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Thu, 6 Jun 2024 19:22:48 +0200 Subject: [PATCH] address comments --- errors/abci_test.go | 10 +--------- errors/errors.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/errors/abci_test.go b/errors/abci_test.go index 3bdc2b34e2c2..ca283997ae91 100644 --- a/errors/abci_test.go +++ b/errors/abci_test.go @@ -67,17 +67,9 @@ func TestABCInfo(t *testing.T) { } } -func TestABCIInfoHidesStacktrace(t *testing.T) { - err := fmt.Errorf("wrapped: %w", ErrUnauthorized) - _, _, log := ABCIInfo(err, false) - if log != "wrapped: unauthorized" { - t.Errorf("expected log %s, got %s", "wrapped: unauthorized", log) - } -} - func TestABCIInfoSerializeErr(t *testing.T) { var ( - // Create errors with stacktrace for equal comparison. + // Create errors for equal comparison. myErrDecode = fmt.Errorf("test: %w", ErrTxDecode) myErrAddr = fmt.Errorf("tester: %w", ErrInvalidAddress) myPanic = ErrPanic diff --git a/errors/errors.go b/errors/errors.go index 8528b8f1e8ac..1255e88003de 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -98,3 +98,27 @@ func (e Error) ABCICode() uint32 { func (e Error) Codespace() string { return e.codespace } + +// Wrap extends given error with an additional information. +// +// If the wrapped error does not provide ABCICode method (ie. stdlib errors), +// it will be labeled as internal error. +// +// If err is nil, this returns nil, avoiding the need for an if statement when +// wrapping a error returned at the end of a function +func Wrap(err error, description string) error { + if err == nil { + return nil + } + + return fmt.Errorf("%s: %w", description, err) +} + +// Wrapf extends given error with an additional information. +// +// This function works like Wrap function with additional functionality of +// formatting the input as specified. +func Wrapf(err error, format string, args ...interface{}) error { + desc := fmt.Sprintf(format, args...) + return Wrap(err, desc) +}