【GoCN酷Go推荐】Go错误处理库cockroachdb/errors

GoCN

共 5632字,需浏览 12分钟

 · 2021-07-27

简介

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 实现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

次要错误一般是在处理错误过程中产生的错误

自定义详细错误信息

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()函数增强了包,但是fmt.Print不知道如何打印这种新形式的错误, CockroachDB error 库在错误打印方面花费了大量精力,我们可以使用他来更好的输出错误信息。

更多用法参考官方API


还想了解更多吗?

更多请查看:https://github.com/cockroachdb/errors#How-to-use

欢迎加入我们GOLANG中国社区:https://gocn.vip/


《酷Go推荐》招募:


各位Gopher同学,最近我们社区打算推出一个类似GoCN每日新闻的新栏目《酷Go推荐》,主要是每周推荐一个库或者好的项目,然后写一点这个库使用方法或者优点之类的,这样可以真正的帮助到大家能够学习到

新的库,并且知道怎么用。


大概规则和每日新闻类似,如果报名人多的话每个人一个月轮到一次,欢迎大家报名!(报名地址:https://wj.qq.com/s2/7734329/3f51)


扫码也可以加入 GoCN 的大家族哟~





浏览 11
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报