-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
53 lines (44 loc) · 783 Bytes
/
std.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
53
package ge
import (
"errors"
)
type UnwrapError interface {
Unwrap() error
}
type UnwrapErrors interface {
Unwrap() []error
}
func Unwrap(err error) error {
u, ok := err.(UnwrapError)
if !ok {
return nil
}
return u.Unwrap()
}
func UnwrapAll(err error) []error {
u, ok := err.(UnwrapErrors)
if !ok {
return nil
}
return u.Unwrap()
}
func Is(err error, target error) bool {
return errors.Is(err, target)
}
func Join(errs ...error) (err error) {
err = errors.Join(errs...)
if errs = UnwrapAll(err); len(errs) == 1 {
return errs[0]
}
return err
}
func As[T any](err error) (T, bool) {
var val T
return val, errors.As(err, &val)
}
func ErrOf(errorable any) error {
if e, ok := errorable.(interface{ Err() error }); ok {
return e.Err()
}
return nil
}