From e672b3f8e176e08e52551f65559d75745647f7e1 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 11 Jul 2022 23:04:38 +0800 Subject: [PATCH] feat: add Wrap and Wrapf in errorx (#2126) --- core/errorx/wrap.go | 21 +++++++++++++++++++++ core/errorx/wrap_test.go | 18 ++++++++++++++++++ gateway/config.go | 7 +++++++ gateway/server.go | 6 ++++++ 4 files changed, 52 insertions(+) create mode 100644 core/errorx/wrap.go create mode 100644 core/errorx/wrap_test.go create mode 100644 gateway/config.go create mode 100644 gateway/server.go diff --git a/core/errorx/wrap.go b/core/errorx/wrap.go new file mode 100644 index 00000000..f4c7da66 --- /dev/null +++ b/core/errorx/wrap.go @@ -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) +} diff --git a/core/errorx/wrap_test.go b/core/errorx/wrap_test.go new file mode 100644 index 00000000..088bc378 --- /dev/null +++ b/core/errorx/wrap_test.go @@ -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()) +} diff --git a/gateway/config.go b/gateway/config.go new file mode 100644 index 00000000..133ebbb7 --- /dev/null +++ b/gateway/config.go @@ -0,0 +1,7 @@ +package gateway + +import "github.com/zeromicro/go-zero/rest" + +type GatewayConf struct { + rest.RestConf +} diff --git a/gateway/server.go b/gateway/server.go new file mode 100644 index 00000000..8cdb1f96 --- /dev/null +++ b/gateway/server.go @@ -0,0 +1,6 @@ +package gateway + +type Server struct { +} + +func MustNewServer() {}