Skip to content

Commit 35a82e5

Browse files
committed
feat: add util function to convert errors list to ExecutionErrors.
1 parent fb12329 commit 35a82e5

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

error.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ type ExecutionError interface {
2727
Error() string
2828
}
2929

30+
// executionError is the error to represents the error of the function that is returned or
31+
// panicked, and the index of the function in the parameters list.
3032
type executionError struct {
33+
// index is the index of the function in the parameters list.
3134
index int
32-
err error
35+
// err is the error that the function returned or panicked.
36+
err error
3337
}
3438

3539
// Index returns the function's index in the parameters list that the function had returned an
@@ -62,3 +66,31 @@ func (ee ExecutionErrors) Error() string {
6266

6367
return strings.TrimSpace(buf.String())
6468
}
69+
70+
// convertErrorListToExecutionErrors converts an array of the errors to the ExecutionErrors, it
71+
// will set the index as the execution error's index. If the error in the list is nil, it will skip
72+
// it and not add the error to the ExecutionErrors.
73+
func convertErrorListToExecutionErrors(errs []error, num int) ExecutionErrors {
74+
if num == 0 {
75+
num = len(errs)
76+
}
77+
78+
ee := make(ExecutionErrors, 0, num)
79+
80+
for i, e := range errs {
81+
if e == nil {
82+
continue
83+
}
84+
85+
ee = append(ee, &executionError{
86+
index: i,
87+
err: e,
88+
})
89+
}
90+
91+
if len(ee) == 0 {
92+
return nil
93+
}
94+
95+
return ee
96+
}

error_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,27 @@ func TestExecutionErrors(t *testing.T) {
3838
a.EqualNow(ee.Error(), `function 0 error: inner error 1
3939
function 1 error: inner error 2`)
4040
}
41+
42+
func TestConvertErrorListToExecutionErrors(t *testing.T) {
43+
a := assert.New(t)
44+
45+
a.NilNow(convertErrorListToExecutionErrors(nil, 0))
46+
a.NilNow(convertErrorListToExecutionErrors(nil, 1))
47+
a.NilNow(convertErrorListToExecutionErrors([]error{}, 0))
48+
a.NilNow(convertErrorListToExecutionErrors([]error{}, 1))
49+
a.NilNow(convertErrorListToExecutionErrors([]error{nil}, 0))
50+
a.NilNow(convertErrorListToExecutionErrors([]error{nil}, 1))
51+
a.EqualNow(
52+
convertErrorListToExecutionErrors([]error{errors.New("expected error")}, 1).Error(),
53+
"function 0 error: expected error")
54+
a.EqualNow(
55+
convertErrorListToExecutionErrors([]error{errors.New("expected error"), nil}, 0).Error(),
56+
"function 0 error: expected error")
57+
a.EqualNow(
58+
convertErrorListToExecutionErrors([]error{
59+
errors.New("expected error 1"),
60+
errors.New("expected error 2"),
61+
}, 1).Error(),
62+
`function 0 error: expected error 1
63+
function 1 error: expected error 2`)
64+
}

0 commit comments

Comments
 (0)