Skip to content

Commit a116214

Browse files
committed
feat: add unwrap method to execution error and errors.
Signed-off-by: ghosind <ghosind@gmail.com>
1 parent 5114388 commit a116214

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

error.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (e *executionError) Error() string {
5959
return fmt.Sprintf("function %d error: %s", e.index, e.err.Error())
6060
}
6161

62+
// Unwrap returns the inner error.
63+
func (e *executionError) Unwrap() error {
64+
return e.err
65+
}
66+
6267
// ExecutionErrors is an array of ExecutionError.
6368
type ExecutionErrors []ExecutionError
6469

@@ -76,6 +81,15 @@ func (ee ExecutionErrors) Error() string {
7681
return strings.TrimSpace(buf.String())
7782
}
7883

84+
// Unwrap returns the execution errors.
85+
func (ee ExecutionErrors) Unwrap() []error {
86+
errs := make([]error, 0, len(ee))
87+
for _, e := range ee {
88+
errs = append(errs, e)
89+
}
90+
return errs
91+
}
92+
7993
// convertErrorListToExecutionErrors converts an array of the errors to the ExecutionErrors, it
8094
// will set the index as the execution error's index. If the error in the list is nil, it will skip
8195
// it and not add the error to the ExecutionErrors.

error_go120_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package async
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/ghosind/go-assert"
8+
)
9+
10+
func TestUnwrapExecutionsError(t *testing.T) {
11+
a := assert.New(t)
12+
13+
innerErr := errors.New("expected error")
14+
err := &executionError{
15+
err: innerErr,
16+
index: 0,
17+
}
18+
19+
errs := ExecutionErrors{err}
20+
a.TrueNow(errors.Is(errs, err))
21+
a.TrueNow(errors.Is(errs, innerErr))
22+
}

error_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,16 @@ func TestConvertErrorListToExecutionErrors(t *testing.T) {
6262
`function 0 error: expected error 1
6363
function 1 error: expected error 2`)
6464
}
65+
66+
func TestUnwrapExecutionError(t *testing.T) {
67+
a := assert.New(t)
68+
69+
innerErr := errors.New("expected error")
70+
err := &executionError{
71+
err: innerErr,
72+
index: 0,
73+
}
74+
75+
a.TrueNow(errors.Is(err, innerErr))
76+
a.NotTrueNow(errors.Is(err, errors.New("unexpected error")))
77+
}

0 commit comments

Comments
 (0)