Go源码分析(2) - errors

Go源码分析(2) - errors

错误类型处理在go中是非常重要的,但是代码不多,本文将分析源码内容。

errors.go

error是一个内建的类型,实现了error接口

1
2
3
type error interface {
Error() string
}

errors包下通过New创建一个error对象,事实上返回的就是一个实现了error接口类型的结构体,其中Error()返回了该错误信息的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}

// errorString is a trivial implementation of error.
type errorString struct {
s string
}

func (e *errorString) Error() string {
return e.s
}

warp.go

在warp文件下,有三个方法,分别为Unwrap(err error) error,Is(err, target error) bool,As(err error, target interface{}) bool
,都是在1.13新加入的方法。

Unwrap(err error) error使用该方法,需要传入的error类型实现Unwrap() error

  • 通过判断是否实现了Unwrap() error
    • 是,调用Unwrap()方法,返回err
    • 否,返回nil
1
2
3
4
5
6
7
8
9
10
11
12
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
func Unwrap(err error) error {
u, ok := err.(interface {
Unwrap() error
})
if !ok {
return nil
}
return u.Unwrap()
}

Is(err, target error) bool判断该错误类型是否和目标类型是一致类型

  1. 如果目标类型为nil,则判断被比较类型是否也是nil
  2. 通过一个for循环不断判断
    1. 如果目标类型能被比较并且被比较类型和目标类型值相等就返回true
    2. 如果被比较类型实现了Is(error) bool接口,则调用接口方法,判断是否是相等的
    3. 解包被比较类型,继续下一轮循环,或者解包到最里层都没有寻找到被比较类型与目标类型可匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Is reports whether any error in err's chain matches target.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool {
if target == nil {
return err == target
}

isComparable := reflectlite.TypeOf(target).Comparable()
for {
if isComparable && err == target {
return true
}
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
// TODO: consider supporing target.Is(err). This would allow
// user-definable predicates, but also may allow for coping with sloppy
// APIs, thereby making it easier to get away with them.
if err = Unwrap(err); err == nil {
return false
}
}
}

As(err error, target interface{}) boolAs方法应将目标设置为其值,如果err匹配目标指向的类型,则返回true。

  1. 如果目标类型为nil,或者不是指针类型,或者该类型没有实现error接口,则panic
  2. 通过一个for循环不断判断
    1. 如果被比较类型能被目标类型赋值,就进行赋值,返回true
    2. 如果被比较类型实现了As(interface{}) bool接口,则调用接口方法,进行赋值,并返回true
    3. 解包被比较类型,继续下一轮循环,或者解包到最里层都没有寻找到实现的As方法都没有赋值成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// As finds the first error in err's chain that matches the type to which target
// points, and if so, sets the target to its value and returns true. An error
// matches a type if it is assignable to the target type, or if it has a method
// As(interface{}) bool such that As(target) returns true. As will panic if
// target is not a non-nil pointer to a type which implements error or is of
// interface type. As returns false if error is nil.
//
// The As method should set the target to its value and return true if err
// matches the type to which target points.
func As(err error, target interface{}) bool {
if target == nil {
panic("errors: target cannot be nil")
}
val := reflectlite.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflectlite.Ptr || val.IsNil() {
panic("errors: target must be a non-nil pointer")
}
if e := typ.Elem(); e.Kind() != reflectlite.Interface && !e.Implements(errorType) {
panic("errors: *target must be interface or implement error")
}
targetType := typ.Elem()
for err != nil {
if reflectlite.TypeOf(err).AssignableTo(targetType) {
val.Elem().Set(reflectlite.ValueOf(err))
return true
}
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) {
return true
}
err = Unwrap(err)
}
return false
}

var errorType = reflectlite.TypeOf((*error)(nil)).Elem()

评论

`
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×