-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
119 lines (100 loc) · 3.23 KB
/
error.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
package async
import (
"bytes"
"errors"
"fmt"
"strings"
)
var (
// ErrContextCanceled to indicate the context was canceled or timed out.
ErrContextCanceled error = errors.New("context canceled")
// ErrInvalidConcurrency to indicate the number of the concurrency limitation is an invalid
// value.
ErrInvalidConcurrency error = errors.New("invalid concurrency")
// ErrNotFunction indicates the value is not a function.
ErrNotFunction error = errors.New("not function")
// ErrUnmatchedParam indicates the function's parameter list does not match to the list from the
// caller.
ErrUnmatchedParam error = errors.New("parameters are unmatched")
// ErrInvalidTestFunc indicates the test function is invalid.
ErrInvalidTestFunc error = errors.New("invalid test function")
// ErrInvalidSeqFuncs indicates the functions in the Seq lists are not match.
ErrInvalidSeqFuncs error = errors.New("invalid seq functions")
)
type ExecutionError interface {
// Index returns the function's index in the parameters list that the function had returned an
// error or panicked.
Index() int
// Err returns the original error that was returned or panicked by the function.
Err() error
// Error returns the execution error message.
Error() string
}
// executionError is the error to represents the error of the function that is returned or
// panicked, and the index of the function in the parameters list.
type executionError struct {
// index is the index of the function in the parameters list.
index int
// err is the error that the function returned or panicked.
err error
}
// Index returns the function's index in the parameters list that the function had returned an
// error or panicked.
func (e *executionError) Index() int {
return e.index
}
// Err returns the original error that was returned or panicked by the function.
func (e *executionError) Err() error {
return e.err
}
// Error returns the execution error message.
func (e *executionError) Error() string {
return fmt.Sprintf("function %d error: %s", e.index, e.err.Error())
}
// Unwrap returns the inner error.
func (e *executionError) Unwrap() error {
return e.err
}
// ExecutionErrors is an array of ExecutionError.
type ExecutionErrors []ExecutionError
// Error combines and returns all of the execution errors' message.
func (ee ExecutionErrors) Error() string {
buf := bytes.NewBufferString("")
for i, e := range ee {
buf.WriteString(e.Error())
if i < len(ee)-1 {
buf.WriteByte('\n')
}
}
return strings.TrimSpace(buf.String())
}
// Unwrap returns the execution errors.
func (ee ExecutionErrors) Unwrap() []error {
errs := make([]error, 0, len(ee))
for _, e := range ee {
errs = append(errs, e)
}
return errs
}
// convertErrorListToExecutionErrors converts an array of the errors to the ExecutionErrors, it
// will set the index as the execution error's index. If the error in the list is nil, it will skip
// it and not add the error to the ExecutionErrors.
func convertErrorListToExecutionErrors(errs []error, num int) ExecutionErrors {
if num == 0 {
num = len(errs)
}
ee := make(ExecutionErrors, 0, num)
for i, e := range errs {
if e == nil {
continue
}
ee = append(ee, &executionError{
index: i,
err: e,
})
}
if len(ee) == 0 {
return nil
}
return ee
}