-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy patherror.go
52 lines (42 loc) · 1.13 KB
/
error.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2023 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package checkers
import (
"errors"
"fmt"
"reflect"
gc "gopkg.in/check.v1"
)
type errorIsChecker struct {
*gc.CheckerInfo
}
// ErrorIs checks whether a value is an error that matches the other
// argument.
var ErrorIs gc.Checker = &errorIsChecker{
CheckerInfo: &gc.CheckerInfo{
Name: "ErrorIs",
Params: []string{"obtained", "error"},
},
}
var (
errType = reflect.TypeOf((*error)(nil)).Elem()
)
func (checker *errorIsChecker) Check(params []interface{}, names []string) (result bool, err string) {
if params[1] == nil || params[0] == nil {
return params[1] == params[0], ""
}
f := reflect.ValueOf(params[1])
ft := f.Type()
if !ft.Implements(errType) {
return false, fmt.Sprintf("wrong error target type, got: %s", ft)
}
v := reflect.ValueOf(params[0])
vt := v.Type()
if !v.IsValid() {
return false, fmt.Sprintf("wrong argument type %s for %s", vt, ft)
}
if !vt.Implements(errType) {
return false, fmt.Sprintf("wrong argument type %s for %s", vt, ft)
}
return errors.Is(v.Interface().(error), f.Interface().(error)), ""
}