-
Notifications
You must be signed in to change notification settings - Fork 191
/
util.go
123 lines (103 loc) · 2.49 KB
/
util.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package errorx
import (
"errors"
"fmt"
)
// E new a raw go error. alias of errors.New()
func E(msg string) error {
return errors.New(msg)
}
// Err new a raw go error. alias of errors.New()
func Err(msg string) error {
return errors.New(msg)
}
// Raw new a raw go error. alias of errors.New()
func Raw(msg string) error {
return errors.New(msg)
}
// Ef new a raw go error. alias of errors.New()
func Ef(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
}
// Errf new a raw go error. alias of errors.New()
func Errf(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
}
// Rawf new a raw go error. alias of errors.New()
func Rawf(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
}
/*************************************************************
* helper func for error
*************************************************************/
// Cause returns the first cause error by call err.Cause().
// Otherwise, will returns current error.
func Cause(err error) error {
if err == nil {
return nil
}
if err, ok := err.(Causer); ok {
return err.Cause()
}
return err
}
// Unwrap returns previous error by call err.Unwrap().
// Otherwise, will returns nil.
func Unwrap(err error) error {
if err == nil {
return nil
}
if err, ok := err.(Unwrapper); ok {
return err.Unwrap()
}
return nil
}
// Previous alias of Unwrap()
func Previous(err error) error { return Unwrap(err) }
// IsErrorX check
func IsErrorX(err error) (ok bool) {
_, ok = err.(*ErrorX)
return
}
// ToErrorX convert check
func ToErrorX(err error) (ex *ErrorX, ok bool) {
ex, ok = err.(*ErrorX)
return
}
// MustEX convert error to *ErrorX, panic if err check failed.
func MustEX(err error) *ErrorX {
ex, ok := err.(*ErrorX)
if !ok {
panic("errorx: error is not *ErrorX")
}
return ex
}
// Has contains target error, or err is eq target.
// alias of errors.Is()
func Has(err, target error) bool {
return errors.Is(err, target)
}
// Is alias of errors.Is()
func Is(err, target error) bool {
return errors.Is(err, target)
}
// To try convert err to target, returns is result.
//
// NOTICE: target must be ptr and not nil. alias of errors.As()
//
// Usage:
//
// var ex *errorx.ErrorX
// err := doSomething()
// if errorx.To(err, &ex) {
// fmt.Println(ex.GoString())
// }
func To(err error, target any) bool {
return errors.As(err, target)
}
// As same of the To(), alias of errors.As()
//
// NOTICE: target must be ptr and not nil
func As(err error, target any) bool {
return errors.As(err, target)
}