Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Wrap and Wrapf in errorx #2126

Merged
merged 1 commit into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions 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)
}
18 changes: 18 additions & 0 deletions 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())
}
7 changes: 7 additions & 0 deletions gateway/config.go
@@ -0,0 +1,7 @@
package gateway

import "github.com/zeromicro/go-zero/rest"

type GatewayConf struct {
rest.RestConf
}
6 changes: 6 additions & 0 deletions gateway/server.go
@@ -0,0 +1,6 @@
package gateway

type Server struct {
}

func MustNewServer() {}