-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerrors.go
62 lines (51 loc) · 1.36 KB
/
zerrors.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
package zerrors
import (
"fmt"
"reflect"
)
// IsType Check if a given error is matching given type
// Example:
// zerrors.IsType(err, MyErrorTypeWithStringerInterface)
func IsType(err TypeAwareError, errType fmt.Stringer) bool {
isComparable := reflect.TypeOf(err.Type()).Comparable()
for {
if isComparable && err.Type() == errType {
return true
}
shouldConvert := Unwrap(err)
if shouldConvert == nil {
return false
}
// TODO: Check if this is suitable or has to be checked
err = shouldConvert.(TypeAwareError) //nolint:forcetypeassert
}
}
// Mask simply masks an error with no message.
func Mask(err error) error {
return NewWithOpts("", WithWrappedError(err), WithSkipCallers(getCallerSkip(1)))
}
// New creates a new error with message.
func New(msg string) error {
return NewWithOpts(msg)
}
// Newf creates a new error with formatted message.
func Newf(format string, a ...interface{}) error {
return NewWithOpts(fmt.Sprintf(format, a...))
}
// NewWithOpts creates a new error with optional passed options
// see: ./options.go for available options
func NewWithOpts(msg string, opts ...ErrorOpt) error {
// Create error with sensitive defaults
e := &zError{
msg: msg,
errType: GenericError,
err: nil,
frame: Caller(DefaultSkipCallers),
ctx: nil,
}
// Apply options
for _, opt := range opts {
opt(e)
}
return e
}