-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
142 lines (116 loc) · 2.98 KB
/
common.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package Errors
import (
"errors"
"io/fs"
"log"
"os"
)
const (
// DefaultLFPermissions is the default file permissions for creating a log file.
DefaultLFPermissions fs.FileMode = 0666
// DefaultLFExtension is the default file extension for creating a log file.
DefaultLFExtension string = ".log.md"
// DefaultLFFlags is the default flags for creating a log file.
DefaultLFFlags int = os.O_CREATE | os.O_WRONLY | os.O_APPEND
// DefaultLoggerFlags is the default flags for creating a logger.
DefaultLoggerFlags int = log.LstdFlags | log.Llongfile
)
// Unwrapper is an interface that defines a method to unwrap an error.
type Unwrapper interface {
// Unwrap returns the error that this error wraps.
//
// Returns:
// - error: The error that this error wraps.
Unwrap() error
// ChangeReason changes the reason of the error.
//
// Parameters:
// - reason: The new reason of the error.
ChangeReason(reason error)
}
// As is function that checks if an error is of type T.
//
// If the error is nil, the function returns false.
//
// Parameters:
// - err: The error to check.
//
// Returns:
// - bool: true if the error is of type T, false otherwise.
func As[T any](err error) bool {
if err == nil {
return false
}
var target T
return errors.As(err, &target)
}
// IsNoError checks if an error is a no error error or if it is nil.
//
// Parameters:
// - err: The error to check.
//
// Returns:
// - bool: True if the error is a no error error or if it is nil, otherwise false.
func IsNoError(err error) bool {
if err == nil {
return true
}
var errNoError *ErrNoError
return errors.As(err, &errNoError)
}
// IsErrIgnorable checks if an error is an *ErrIgnorable or *ErrInvalidParameter error.
// If the error is nil, the function returns false.
//
// Parameters:
// - err: The error to check.
//
// Returns:
// - bool: True if the error is an *ErrIgnorable or *ErrInvalidParameter error,
// otherwise false.
func IsErrIgnorable(err error) bool {
if err == nil {
return false
}
var ignorable *ErrIgnorable
if errors.As(err, &ignorable) {
return true
}
var invalid *ErrInvalidParameter
return errors.As(err, &invalid)
}
// LimitErrorMsg limits the error message to a certain number of unwraps.
// It returns the top level error for allowing to print the error message
// with the limit of unwraps applied.
//
// If the error is nil or the limit is less than 0, the function does nothing.
//
// Parameters:
// - err: The error to limit.
// - limit: The limit of unwraps.
//
// Returns:
// - error: The top level error with the limit of unwraps applied.
func LimitErrorMsg(err error, limit int) error {
if err == nil || limit < 0 {
return err
}
currErr := err
for i := 0; i < limit; i++ {
target, ok := currErr.(Unwrapper)
if !ok {
return err
}
reason := target.Unwrap()
if reason == nil {
return err
}
currErr = reason
}
// Limit reached
target, ok := currErr.(Unwrapper)
if !ok {
return err
}
target.ChangeReason(nil)
return err
}