-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,69 @@ | ||
package gerror | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Error struct { | ||
Code int | ||
Msg string | ||
} | ||
|
||
func (err Error) Error() string { | ||
return err.Msg | ||
// Error 方法实现了error接口,返回错误信息 | ||
func (e Error) Error() string { | ||
return e.Msg | ||
} | ||
|
||
// Wrap 用于包装错误信息 | ||
func (e Error) Wrap(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
// 保存原始错误信息 | ||
msg := e.Msg | ||
// 更新错误信息,方便后续使用 | ||
e.Msg = err.Error() | ||
return errors.Wrap(err, msg) | ||
} | ||
|
||
func (e Error) Wrapf(err error, format string, args ...interface{}) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
// 保存原始错误信息 | ||
msg := e.Msg | ||
formattedMsg := fmt.Sprintf(format, args...) | ||
e.Msg = err.Error() | ||
return errors.Wrap(err, fmt.Sprintf("%s %s", formattedMsg, msg)) | ||
} | ||
|
||
func (e Error) GetCode() int { | ||
return e.Code | ||
} | ||
|
||
func (e Error) GetMsg() string { | ||
return e.Msg | ||
} | ||
|
||
// ResetMsg 重置错误信息 | ||
func (e Error) ResetMsg(msg string) { | ||
e.Msg = msg | ||
} | ||
|
||
// AppendMsg 在既有错误信息的基础上追加新的错误信息 | ||
func (e Error) AppendMsg(args ...interface{}) { | ||
e.Msg += fmt.Sprint(args...) | ||
} | ||
|
||
// Is 判断是否为指定错误类型 | ||
func (e Error) Is(targetErr error) bool { | ||
return errors.Is(targetErr, e) | ||
} | ||
|
||
// As 判断是否为指定错误类型,并将错误信息赋值给目标变量 | ||
func (e Error) As(targetErr interface{}) bool { | ||
return errors.As(e, targetErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package gerror | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestWrap(t *testing.T) { | ||
err1 := Error{ | ||
Code: 1, | ||
Msg: "test1", | ||
} | ||
err2 := Error{ | ||
Code: 2, | ||
Msg: "test2", | ||
} | ||
err := err1.Wrap(err2) | ||
t.Log(err) | ||
} | ||
|
||
func TestWrapf(t *testing.T) { | ||
err1 := Error{ | ||
Code: 1, | ||
Msg: "test1", | ||
} | ||
err2 := Error{ | ||
Code: 2, | ||
Msg: "test2", | ||
} | ||
err := err1.Wrapf(err2, "here is errMsg:%s", "123") | ||
t.Log(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters