-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
108 lines (86 loc) · 2.17 KB
/
helper.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
package errors
import (
stderrors "errors"
"fmt"
"go.uber.org/multierr"
)
// New is a function wrapper for stdlib/errors.New to avoid imports conflict.
func New(text string) error {
return stderrors.New(text)
}
// Errorf is a function wrapper for stdlib/fmt.Errorf to avoid imports conflict.
func Errorf(format string, a ...interface{}) error {
return fmt.Errorf(format, a...)
}
// As is a function wrapper for stdlib/errors.As to avoid imports conflict.
func As(err error, target interface{}) bool {
if stderrors.As(err, target) {
return true
}
// compatibility with github.com/pkg/errors
if e := Cause(err); e != err && stderrors.As(e, target) {
return true
}
return false
}
// Is is a function wrapper for stdlib/errors.Is to avoid imports conflict.
func Is(err, target error) bool {
if stderrors.Is(err, target) {
return true
}
// compatibility with github.com/pkg/errors
if e := Cause(err); e != err && stderrors.Is(e, target) {
return true
}
return false
}
// IsOneOf checks if the error is one of the targets
func IsOneOf(err error, target ...error) bool {
for _, t := range target {
if Is(err, t) {
return true
}
}
return false
}
// Unwrap is a function wrapper for stdlib/errors.Unwrap to avoid imports conflict.
func Unwrap(err error) error {
return stderrors.Unwrap(err)
}
// Wrapf returns an error with annotation
func Wrapf(err error, msg string, args ...interface{}) error {
if err != nil && msg != "" {
description := fmt.Sprintf(msg, args...)
return Errorf(description+": %w", err)
}
return err
}
// Wrap returns an error with annotation
func Wrap(err error, msg string) error {
if err != nil && msg != "" {
return Errorf(msg+": %w", err)
}
return err
}
// Errors returns a list of errors if it is a multiple error
func Errors(err error) []error {
if err == nil {
return nil
}
return multierr.Errors(err)
}
// Cause is a method for a compatibility with https://pkg.go.dev/github.com/pkg/errors#Cause
// Deprecated. Please use the method Unwrap.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}