feat: add Wrap and Wrapf in errorx (#2126)

This commit is contained in:
Kevin Wan 2022-07-11 23:04:38 +08:00 committed by GitHub
parent 1c09db6d5d
commit e672b3f8e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

21
core/errorx/wrap.go Normal file
View File

@ -0,0 +1,21 @@
package errorx
import "fmt"
// Wrap returns an error that wraps err with given message.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", message, err)
}
// Wrapf returns an error that wraps err with given format and args.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err)
}

18
core/errorx/wrap_test.go Normal file
View File

@ -0,0 +1,18 @@
package errorx
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWrap(t *testing.T) {
assert.Nil(t, Wrap(nil, "test"))
assert.Equal(t, "foo: bar", Wrap(errors.New("bar"), "foo").Error())
}
func TestWrapf(t *testing.T) {
assert.Nil(t, Wrapf(nil, "%s", "test"))
assert.Equal(t, "foo bar: quz", Wrapf(errors.New("quz"), "foo %s", "bar").Error())
}

7
gateway/config.go Normal file
View File

@ -0,0 +1,7 @@
package gateway
import "github.com/zeromicro/go-zero/rest"
type GatewayConf struct {
rest.RestConf
}

6
gateway/server.go Normal file
View File

@ -0,0 +1,6 @@
package gateway
type Server struct {
}
func MustNewServer() {}