【GoCN酷Go推薦】Go錯誤處理庫cockroachdb/errors
CockroachDB errors 是Go標準庫errors、github.com/pkg/errors的通用替代品
提供簡單錯誤信息和詳細堆棧跟蹤信息 添加消息前綴 次要錯誤 檢查錯誤類型 支持自定義錯誤詳細信息
基本使用
import "github.com/cockroachdb/errors"
err := errors.New("msg")
fmt.Println(err)
運行,輸出結果:
$ msg
錯誤堆棧跟蹤
err := errors.New("msg")
fmt.Printf("%+v", err)
運行,輸出結果:
$ msg
(1) attached stack trace
-- stack trace:
| main.main
| /Your Path/main.go:37
| runtime.main
| /usr/local/Cellar/go/1.16/libexec/src/runtime/proc.go:225
| runtime.goexit
| /usr/local/Cellar/go/1.16/libexec/src/runtime/asm_amd64.s:1371
Wraps: (2) err
Error types: (1) *withstack.withStack (2) *errutil.leafError%
添加消息前綴
err1 := errors.New("msg")
err2 := errors.Wrap(err1, "another err")
fmt.Printf("%+v", err2)
運行,輸出結果:
another err: msg
(1) attached stack trace
-- stack trace:
| main.main
| /YourPath/main.go:38
| [...repeated from below...]
Wraps: (2) another err
Wraps: (3) attached stack trace
-- stack trace:
| main.main
| /YourPath/main.go:37
| runtime.main
| /usr/local/Cellar/go/1.16/libexec/src/runtime/proc.go:225
| runtime.goexit
| /usr/local/Cellar/go/1.16/libexec/src/runtime/asm_amd64.s:1371
Wraps: (4) err
Error types: (1) *withstack.withStack (2) *errutil.withPrefix (3) *withstack.withStack (4) *errutil.leafError%
檢查錯誤類型
// CustomErr 實現(xiàn)error interface
type CustomErr struct{}
func (*CustomErr) Error() string {
return "custom err"
}
err1 := &CustomErr{}
err2 := errors.Wrap(err1, "msg")
err3 := errors.New("msg")
fmt.Println(errors.Is(err2, err1), errors.Is(err3, err1), errors.Is(err2, err1))
運行,輸出結果:
true false true
在go1.13以后錯誤都是鏈表,這個方法可以很方便的檢查錯誤類型
次要錯誤
err := errors.New("msg")
err2 := errors.WithSecondaryError(err, errors.New("secondary err"))
fmt.Printf("%+v", err2)
運行,輸出結果:
$ msg
(1) secondary error attachment
| secondary err
| (1) attached stack trace
| -- stack trace:
| | main.main
| | /YourPath/main.go:38
| | runtime.main
| | /usr/local/Cellar/go/1.16/libexec/src/runtime/proc.go:225
| | runtime.goexit
| | /usr/local/Cellar/go/1.16/libexec/src/runtime/asm_amd64.s:1371
| Wraps: (2) secondary err
| Error types: (1) *withstack.withStack (2) *errutil.leafError
Wraps: (2) attached stack trace
-- stack trace:
| main.main
| /YourPath/main.go:37
| runtime.main
| /usr/local/Cellar/go/1.16/libexec/src/runtime/proc.go:225
| runtime.goexit
| /usr/local/Cellar/go/1.16/libexec/src/runtime/asm_amd64.s:1371
Wraps: (3) err
Error types: (1) *secondary.withSecondaryError (2) *withstack.withStack (3) *errutil.leafError
次要錯誤一般是在處理錯誤過程中產(chǎn)生的錯誤
自定義詳細錯誤信息
type CustomErr struct {
Cause error
Code int
}
func (*CustomErr) Error() string {
return "custom err"
}
func (c *CustomErr) Format(s fmt.State, verb rune) { errors.FormatError(c, s, verb) }
func (c *CustomErr) SafeFormatError(p errors.Printer) (next error) {
if p.Detail() {
p.Printf("http code: %d", errors.Safe(c.Code))
}
return c.Cause
}
err := errors.New("msg")
err1 := &CustomErr{
Cause: err,
Code: 500,
}
fmt.Printf("%+v", err1)
運行,結果:
(1) http code: 500
Error types: (1) *main.CustomErr
總結
Go1.13開始將錯誤視為鏈表,對Unwrap()方法進行標準化,并使用了Is()和As()函數(shù)增強了包,但是fmt.Print不知道如何打印這種新形式的錯誤, CockroachDB error 庫在錯誤打印方面花費了大量精力,我們可以使用他來更好的輸出錯誤信息。
更多用法參考官方API
更多請查看:https://github.com/cockroachdb/errors#How-to-use
歡迎加入我們GOLANG中國社區(qū):https://gocn.vip/
《酷Go推薦》招募:
各位Gopher同學,最近我們社區(qū)打算推出一個類似GoCN每日新聞的新欄目《酷Go推薦》,主要是每周推薦一個庫或者好的項目,然后寫一點這個庫使用方法或者優(yōu)點之類的,這樣可以真正的幫助到大家能夠學習到
新的庫,并且知道怎么用。
大概規(guī)則和每日新聞類似,如果報名人多的話每個人一個月輪到一次,歡迎大家報名!(報名地址:https://wj.qq.com/s2/7734329/3f51)
掃碼也可以加入 GoCN 的大家族喲~
評論
圖片
表情
