为了方便分享,特地把blog抄到这里 = =
实在是对代码中到处打印错误日志的现象难以忍受, 于是琢磨了一个优雅一些的错误处理方式. 特地整理出来分享一下.
源码地址: https://github.com/mozhata/merr
需求
我想知道原始错误出现在哪里, 还有对应的函数调用栈是怎样的
我想给某个函数返回的错误加上点其他信息再返回,但是又想保留原始错误信息
可能还需要一个状态码 用来标识某一类错误
我想...算了,想太多不好
打印出来大概就是这么个效果
E500: err: new err raw err: origin err call stack: main.warpper practice/go/example/main.go:18main.main practice/go/example/main.go:12runtime.main runtime/proc.go:183runtime.goexit runtime/asm_amd64.s:2086
(附打印函数)
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(),
)
}关键代码
首先要搞一个错误类
type MErr struct {
Message string // 保存自定义的错误信息
StatusCode int // 错误状态码
rawErr error // 保存原始错误信息
stackPC []uintptr // 保存函数调用栈指针}然后是一些关键的方法
func (e *MErr) Error() string { return e.Message
}// RawErr the origin errfunc (e MErr) RawErr() error { return e.rawErr
}// CallStack get function call stackfunc (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()方法是用来吧函数调用栈指针转换成字符串
还缺一个封装方法
// 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 nilfunc 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 messagefunc 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 ""}可以看出来 这个错误处理的方法主要由fmtErrMsg wrapErr CallStack 这三部分实现
接下来封装几个方便用的函数
// WrapErr equal to InternalErr(err)// notice: be careful, the returned value is *MErr, not errorfunc 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 errorfunc 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 fmtAndArgsfunc NotFoundErr(err error, fmtAndArgs ...interface{}) error { return wrapErr(err, http.StatusNotFound, fmtAndArgs...)
}这基本上就是全部的代码了.没有太多需要解释的地方,基本看代码就明白了. 更详细的用法可以查看测试文件
这是源码地址
作者:mozhata_cf7b
链接:https://www.jianshu.com/p/ee50734e2d62
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦