Skip to content

Commit

Permalink
Wrapf APIs are needed due to govet linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Weber committed Oct 4, 2024
1 parent c855939 commit 89357a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
36 changes: 30 additions & 6 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import (
"github.com/gregwebs/errors"
)

// Wrap calls errors.Wrap or errors.Wrapf on the inner error.
// Wrap calls errors.Wrap on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func Wrap(errCode ErrorCode, msg string, args ...interface{}) ErrorCode {
func Wrap(errCode ErrorCode, msg string) ErrorCode {
//nolint:govet
return wrapG(wrapWith, errCode, msg)
}

// Wrapf calls errors.Wrapf on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func Wrapf(errCode ErrorCode, msg string, args ...interface{}) ErrorCode {
return wrapG(wrapWith, errCode, msg, args...)
}

Expand All @@ -18,10 +26,18 @@ func Wraps(errCode ErrorCode, msg string, args ...interface{}) ErrorCode {
return wrapWith(errCode, errors.WrapsFn(msg, args...))
}

// WrapUser calls errors.Wrap or errors.Wrapf on the inner error.
// WrapUser calls errors.Wrap on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func WrapUser(errCode UserCode, msg string) UserCode {
//nolint:govet
return wrapG(wrapUserWith, errCode, msg)
}

// WrapUserf calls errors.Wrapf on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func WrapUser(errCode UserCode, msg string, args ...interface{}) UserCode {
func WrapfUser(errCode UserCode, msg string, args ...interface{}) UserCode {
return wrapG(wrapUserWith, errCode, msg, args...)
}

Expand All @@ -32,10 +48,18 @@ func WrapsUser(errCode UserCode, msg string, args ...interface{}) UserCode {
return wrapUserWith(errCode, errors.WrapsFn(msg, args...))
}

// WrapOp calls errors.Wrap or errors.Wrapf on the inner error.
// WrapOp calls errors.Wrap on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func WrapOp(errCode OpCode, msg string) OpCode {
//nolint:govet
return wrapG(wrapOpWith, errCode, msg)
}

// WrapOpf calls errors.Wrapf on the inner error.
// This will wrap in place via errors.ErrorWrapper if available
// If a nil is given it is a noop
func WrapOp(errCode OpCode, msg string, args ...interface{}) OpCode {
func WrapfOp(errCode OpCode, msg string, args ...interface{}) OpCode {
return wrapG(wrapOpWith, errCode, msg, args...)
}

Expand Down
4 changes: 2 additions & 2 deletions wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestErrorWrapperFunctions(t *testing.T) {
{
bad := errcode.NewBadRequestErr(underlying)
AssertCode(t, bad, errcode.InvalidInputCode.CodeStr())
coded := errcode.Wrap(bad, "wrapped %s", "arg")
coded := errcode.Wrapf(bad, "wrapped %s", "arg")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "wrapped arg: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestUserWrapperFunctions(t *testing.T) {
errcode.NewBadRequestErr(underlying),
)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
coded = errcode.WrapUser(coded, "wrapped %s", "arg")
coded = errcode.WrapfUser(coded, "wrapped %s", "arg")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "user: wrapped arg: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
Expand Down

0 comments on commit 89357a1

Please sign in to comment.