Skip to content

Commit

Permalink
增加wrap和wrapf函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 17, 2024
1 parent aacef44 commit 01f26e8
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 37 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
path-to-profile: /tmp/coverage/combined.txt
flag-name: Go-${{ matrix.go }}
parallel: true
if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时上传覆盖率

check-coverage:
name: Check coverage
Expand All @@ -51,14 +52,15 @@ jobs:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时检查覆盖率

# 发布 Release
release:
name: Release a new version
needs: [ lint, test ]
runs-on: ubuntu-latest
# 仅在推送标签时执行
if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }}
# 仅在推送标签时执行 - && - 仅在非 fork 时执行发布
if: ${{ github.event.repository.fork == false && success() && startsWith(github.ref, 'refs/tags/v') }}
steps:
# 1. 检出代码
- name: Checkout code
Expand Down
42 changes: 29 additions & 13 deletions eecho/eecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ type Log interface {
DebugLog(msg string, fields ...zap.Field)
}

type EEcho struct {
type ErrorsEcho struct {
log Log
}

func NewEEcho(log Log) *EEcho {
return &EEcho{log: log}
func NewErrorsEcho(log Log) *ErrorsEcho {
return &ErrorsEcho{log: log}
}

// Ero logs the provided error and returns it.
// Ero 记录提供的错误并返回该错误。
func (x *EEcho) Ero(erx error) error {
func (x *ErrorsEcho) Ero(erx error) error {
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
}
Expand All @@ -29,7 +29,7 @@ func (x *EEcho) Ero(erx error) error {

// New creates a new error with the given message, logs it, and returns the error.
// New 使用给定的消息创建一个新的错误,记录该错误并返回。
func (x *EEcho) New(message string) error {
func (x *ErrorsEcho) New(message string) error {
erx := errors.New(message)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
Expand All @@ -39,7 +39,7 @@ func (x *EEcho) New(message string) error {

// WithMessage adds a custom message to the existing error, logs it, and returns the new error.
// WithMessage 给现有的错误添加自定义消息,记录该错误并返回。
func (x *EEcho) WithMessage(err error, message string) error {
func (x *ErrorsEcho) WithMessage(err error, message string) error {
erx := errors.WithMessage(err, message)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
Expand All @@ -49,7 +49,7 @@ func (x *EEcho) WithMessage(err error, message string) error {

// WithMessagef adds a formatted message to the existing error, logs it, and returns the new error.
// WithMessagef 给现有的错误添加格式化消息,记录该错误并返回。
func (x *EEcho) WithMessagef(err error, format string, args ...interface{}) error {
func (x *ErrorsEcho) WithMessagef(err error, format string, args ...interface{}) error {
erx := errors.WithMessagef(err, format, args...)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
Expand All @@ -59,7 +59,7 @@ func (x *EEcho) WithMessagef(err error, format string, args ...interface{}) erro

// Errorf creates a new formatted error, logs it, and returns the error.
// Errorf 创建一个新的格式化错误,记录该错误并返回。
func (x *EEcho) Errorf(format string, args ...interface{}) error {
func (x *ErrorsEcho) Errorf(format string, args ...interface{}) error {
erx := errors.Errorf(format, args...)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
Expand All @@ -69,13 +69,13 @@ func (x *EEcho) Errorf(format string, args ...interface{}) error {

// Is checks if the provided error is equal to the target error.
// Is 检查提供的错误是否与目标错误相等。
func (x *EEcho) Is(erx, target error) bool {
func (x *ErrorsEcho) Is(erx, target error) bool {
return errors.Is(erx, target)
}

// Ise checks if the provided error is equal to the target error, logging DEBUG for matching errors and ERROR for others.
// Ise 检查提供的错误是否与目标错误相等,对于匹配的错误记录 DEBUG 日志,其他错误记录 ERROR 日志。
func (x *EEcho) Ise(erx, target error) bool {
func (x *ErrorsEcho) Ise(erx, target error) bool {
if errors.Is(erx, target) {
if erx != nil {
x.log.DebugLog("DEBUG", zap.Error(erx))
Expand All @@ -90,13 +90,13 @@ func (x *EEcho) Ise(erx, target error) bool {

// As checks if the provided error can be cast to the target type.
// As 检查提供的错误是否可以转换为目标类型。
func (x *EEcho) As(erx error, target any) bool {
func (x *ErrorsEcho) As(erx error, target any) bool {
return errors.As(erx, target)
}

// Ase checks if the provided error can be cast to the target type, logging DEBUG for successful casts and ERROR for others.
// Ase 检查提供的错误是否可以转换为目标类型,成功转换时记录 DEBUG 日志,其他情况记录 ERROR 日志。
func (x *EEcho) Ase(erx error, target any) bool {
func (x *ErrorsEcho) Ase(erx error, target any) bool {
if errors.As(erx, target) {
if erx != nil {
x.log.DebugLog("DEBUG", zap.Error(erx), zap.Any("target", target))
Expand All @@ -111,10 +111,26 @@ func (x *EEcho) Ase(erx error, target any) bool {

// Wro adds a default "wrong" message to the provided error, logs it, and returns the new error.
// Wro 为提供的错误添加默认的 "wrong" 消息,记录该错误并返回。
func (x *EEcho) Wro(err error) error {
func (x *ErrorsEcho) Wro(err error) error {
erx := errors.WithMessage(err, "wrong")
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
}
return erx
}

func (x *ErrorsEcho) Wrap(err error, message string) error {
erx := errors.Wrap(err, message)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
}
return erx
}

func (x *ErrorsEcho) Wrapf(err error, format string, args ...interface{}) error {
erx := errors.Wrapf(err, format, args...)
if erx != nil {
x.log.ErrorLog("ERROR", zap.Error(erx))
}
return erx
}
36 changes: 28 additions & 8 deletions eecho/eecho_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,52 @@ func (e *echoExample) DebugLog(msg string, fields ...zap.Field) {
zaplog.LOGS.Pn(2).Debug(e.prefix+msg, fields...)
}

var caseEEcho *EEcho
var caseErrorsEcho *ErrorsEcho

func TestMain(m *testing.M) {
caseEEcho = NewEEcho(newEchoExample("echo-message:"))
caseErrorsEcho = NewErrorsEcho(newEchoExample("echo-message:"))
m.Run()
}

func TestEro(t *testing.T) {
require.Error(t, caseEEcho.Ero(errors.New("abc")))
require.Error(t, caseErrorsEcho.Ero(errors.New("abc")))
}

func TestNew(t *testing.T) {
require.Error(t, caseEEcho.New("wrong"))
require.Error(t, caseErrorsEcho.New("wrong"))
}

func TestIse(t *testing.T) {
var erx = errors.New("wrong")

//当错误匹配时,就打印调试日志
require.True(t, caseEEcho.Ise(erx, erx))
require.True(t, caseErrorsEcho.Ise(erx, erx))
//当错误不匹配,就打印错误日志
require.False(t, caseEEcho.Ise(erx, errors.New("wrong")))
require.False(t, caseErrorsEcho.Ise(erx, errors.New("wrong")))
}

func TestWro(t *testing.T) {
erx := errors.New("abc")
require.Error(t, caseEEcho.WithMessage(erx, "wrong"))
require.Error(t, caseEEcho.Wro(erx)) //和前一行等效,能稍微省点代码
require.Error(t, caseErrorsEcho.WithMessage(erx, "wrong"))
require.Error(t, caseErrorsEcho.Wro(erx)) //和前一行等效,能稍微省点代码
}

func TestWithMessage(t *testing.T) {
erx := errors.New("abc")
require.Error(t, caseErrorsEcho.WithMessage(erx, "wrong"))
}

func TestWithMessagef(t *testing.T) {
erx := errors.New("abc")
require.Error(t, caseErrorsEcho.WithMessagef(erx, "wrong reason=%s", "reason"))
}

func TestWrap(t *testing.T) {
erx := errors.New("abc")
require.Error(t, caseErrorsEcho.Wrap(erx, "wrong"))
}

func TestWrapf(t *testing.T) {
erx := errors.New("abc")
require.Error(t, caseErrorsEcho.Wrapf(erx, "wrong reason=%s", "reason"))
}
8 changes: 8 additions & 0 deletions erero.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ func Ase(erx error, target any) bool {
func Wro(erx error) error {
return op.Wro(erx)
}

func Wrap(err error, message string) error {
return op.Wrap(err, message)
}

func Wrapf(err error, format string, args ...interface{}) error {
return op.Wrapf(err, format, args...)
}
35 changes: 28 additions & 7 deletions erero_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
package erero
package erero_test

import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/yyle88/erero"
)

func TestEro(t *testing.T) {
require.Error(t, Ero(errors.New("abc")))
require.Error(t, erero.Ero(errors.New("abc")))
}

func TestNew(t *testing.T) {
require.Error(t, New("wrong"))
require.Error(t, erero.New("wrong"))
}

func TestIse(t *testing.T) {
var erx = errors.New("wrong")

//当错误匹配时,就打印调试日志
require.True(t, Ise(erx, erx))
require.True(t, erero.Ise(erx, erx))
//当错误不匹配,就打印错误日志
require.False(t, Ise(erx, errors.New("wrong")))
require.False(t, erero.Ise(erx, errors.New("wrong")))
}

func TestWro(t *testing.T) {
erx := errors.New("abc")
require.Error(t, WithMessage(erx, "wrong"))
require.Error(t, Wro(erx)) //和前一行等效,能稍微省点代码
require.Error(t, erero.WithMessage(erx, "wrong"))
require.Error(t, erero.Wro(erx)) //和前一行等效,能稍微省点代码
}

func TestWithMessage(t *testing.T) {
erx := errors.New("abc")
require.Error(t, erero.WithMessage(erx, "wrong"))
}

func TestWithMessagef(t *testing.T) {
erx := errors.New("abc")
require.Error(t, erero.WithMessagef(erx, "wrong reason=%s", "reason"))
}

func TestWrap(t *testing.T) {
erx := errors.New("abc")
require.Error(t, erero.Wrap(erx, "wrong"))
}

func TestWrapf(t *testing.T) {
erx := errors.New("abc")
require.Error(t, erero.Wrapf(erx, "wrong reason=%s", "reason"))
}
8 changes: 4 additions & 4 deletions erzap/erzap.go → erzap/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

// ED 当出错时打印 ERROR 级别的日志,当错误满足 Is 的时候会打印 DEBUG 级别日志
var ED = eecho.NewEEcho(NewZapED(2))
var ED = eecho.NewErrorsEcho(NewZapED(2))

// EN 当出错时打印 ERROR 级别的日志,当错误满足 Is 的时候会不打印日志
var EN = eecho.NewEEcho(NewZapEN(2))
var EN = eecho.NewErrorsEcho(NewZapEN(2))

// WD 当出错时打印 WARNING 级别的日志,当错误满足 Is 的时候会打印 DEBUG 级别日志
var WD = eecho.NewEEcho(NewZapWD(2))
var WD = eecho.NewErrorsEcho(NewZapWD(2))

// DD 当出错时打印 DEBUG 级别的日志,当错误满足 Is 的时候会打印 DEBUG 级别日志
var DD = eecho.NewEEcho(NewZapDD(2))
var DD = eecho.NewErrorsEcho(NewZapDD(2))
2 changes: 1 addition & 1 deletion erzap/erzap_test.go → erzap/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/yyle88/erero/eecho"
)

func TestErrors_Imp(t *testing.T) {
func TestErrors_Implement(t *testing.T) {
var _ eecho.Log = NewZapDD(2)
var _ eecho.Log = NewZapED(2)
var _ eecho.Log = NewZapEN(2)
Expand Down
2 changes: 1 addition & 1 deletion vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
"github.com/yyle88/erero/erzap"
)

var op = eecho.NewEEcho(erzap.NewZapED(3))
var op = eecho.NewErrorsEcho(erzap.NewZapED(3))
2 changes: 1 addition & 1 deletion vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestEEcho(t *testing.T) {
func TestExample(t *testing.T) {
require.Error(t, runExample())
}

Expand Down

0 comments on commit 01f26e8

Please sign in to comment.