為了方便分享,特地把blog抄到這里 = =
實(shí)在是對(duì)代碼中到處打印錯(cuò)誤日志的現(xiàn)象難以忍受, 于是琢磨了一個(gè)優(yōu)雅一些的錯(cuò)誤處理方式. 特地整理出來分享一下.
源碼地址: https://github.com/mozhata/merr
需求
- 我想知道原始錯(cuò)誤出現(xiàn)在哪里, 還有對(duì)應(yīng)的函數(shù)調(diào)用棧是怎樣的
- 我想給某個(gè)函數(shù)返回的錯(cuò)誤加上點(diǎn)其他信息再返回,但是又想保留原始錯(cuò)誤信息
- 可能還需要一個(gè)狀態(tài)碼 用來標(biāo)識(shí)某一類錯(cuò)誤
- 我想...算了,想太多不好
打印出來大概就是這么個(gè)效果
E500: err: new err
raw err: origin err
call stack: main.warpper
practice/go/example/main.go:18
main.main
practice/go/example/main.go:12
runtime.main
runtime/proc.go:183
runtime.goexit
runtime/asm_amd64.s:2086
(附打印函數(shù))
func logMerr(err error) {
e := merr.WrapErr(err)
fmt.Printf("E%d: err: %s\nraw err: %s\ncall stack: %s\n",
e.StatusCode,
e.Error(),
e.RawErr(),
e.CallStack(),
)
}
關(guān)鍵代碼
首先要搞一個(gè)錯(cuò)誤類
type MErr struct {
Message string // 保存自定義的錯(cuò)誤信息
StatusCode int // 錯(cuò)誤狀態(tài)碼
rawErr error // 保存原始錯(cuò)誤信息
stackPC []uintptr // 保存函數(shù)調(diào)用棧指針
}
然后是一些關(guān)鍵的方法
func (e *MErr) Error() string {
return e.Message
}
// RawErr the origin err
func (e MErr) RawErr() error {
return e.rawErr
}
// CallStack get function call stack
func (e MErr) CallStack() string {
frames := runtime.CallersFrames(e.stackPC)
var (
f runtime.Frame
more bool
result string
index int
)
for {
f, more = frames.Next()
if index = strings.Index(f.File, "src"); index != -1 {
// trim GOPATH or GOROOT prifix
f.File = string(f.File[index+4:])
}
result = fmt.Sprintf("%s%s\n\t%s:%d\n", result, f.Function, f.File, f.Line)
if !more {
break
}
}
return result
}
其中CallStack()
方法是用來吧函數(shù)調(diào)用棧指針轉(zhuǎn)換成字符串
還缺一個(gè)封裝方法
// maintain rawErr and update Message if fmtAndArgs is not empty
// update StatusCode to code if code is not 0
// notice: the returned value is used as error, so, should not return nil
func wrapErr(err error, code int, fmtAndArgs ...interface{}) *MErr {
msg := fmtErrMsg(fmtAndArgs...)
if err == nil {
err = errors.New(msg)
}
if e, ok := err.(*MErr); ok {
if msg != "" {
e.Message = msg
}
if code != 0 {
e.StatusCode = code
}
return e
}
pcs := make([]uintptr, 32)
// skip the first 3 invocations
count := runtime.Callers(3, pcs)
e := &MErr{
StatusCode: code,
Message: msg,
rawErr: err,
stackPC: pcs[:count],
}
if e.Message == "" {
e.Message = err.Error()
}
return e
}
// fmtErrMsg used to format error message
func fmtErrMsg(msgs ...interface{}) string {
if len(msgs) > 1 {
return fmt.Sprintf(msgs[0].(string), msgs[1:]...)
}
if len(msgs) == 1 {
if v, ok := msgs[0].(string); ok {
return v
}
if v, ok := msgs[0].(error); ok {
return v.Error()
}
}
return ""
}
可以看出來 這個(gè)錯(cuò)誤處理的方法主要由fmtErrMsg
wrapErr
CallStack
這三部分實(shí)現(xiàn)
接下來封裝幾個(gè)方便用的函數(shù)
// WrapErr equal to InternalErr(err)
// notice: be careful, the returned value is *MErr, not error
func WrapErr(err error, fmtAndArgs ...interface{}) *MErr {
return wrapErr(err, http.StatusInternalServerError, fmtAndArgs...)
}
// WrapErrWithCode if code is not 0, update StatusCode to code,
// if fmtAndArgs is not nil, update the Message according to fmtAndArgs
// notice: be careful, the returned value is *MErr, not error
func WrapErrWithCode(err error, code int, fmtAndArgs ...interface{}) *MErr {
return wrapErr(err, code, fmtAndArgs...)
}
// NotFoundErr use http.StatusNotFound as StatusCode to express not found err
// if fmtAndArgs is not nil, update the Message according to fmtAndArgs
func NotFoundErr(err error, fmtAndArgs ...interface{}) error {
return wrapErr(err, http.StatusNotFound, fmtAndArgs...)
}
這基本上就是全部的代碼了.沒有太多需要解釋的地方,基本看代碼就明白了. 更詳細(xì)的用法可以查看測(cè)試文件
這是源碼地址