Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Jun 6, 2024
1 parent d3bf0d2 commit a065237
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
10 changes: 1 addition & 9 deletions errors/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a065237

Please sign in to comment.