Skip to content

Commit aef794a

Browse files
committed
feat: add execution error.
1 parent 894830e commit aef794a

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

error.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package async
22

3-
import "errors"
3+
import (
4+
"errors"
5+
"fmt"
6+
)
47

58
var (
69
// ErrContextCanceled to indicate the context was canceled or timed out.
@@ -11,3 +14,34 @@ var (
1114
// ErrNotFunction indicates the value is not a function.
1215
ErrNotFunction error = errors.New("not function")
1316
)
17+
18+
type ExecutionError interface {
19+
// Index returns the function's index in the parameters list that the function had returned an
20+
// error or panicked.
21+
Index() int
22+
// Err returns the original error that was returned or panicked by the function.
23+
Err() error
24+
// Error returns the execution error message.
25+
Error() string
26+
}
27+
28+
type executionError struct {
29+
index int
30+
err error
31+
}
32+
33+
// Index returns the function's index in the parameters list that the function had returned an
34+
// error or panicked.
35+
func (e *executionError) Index() int {
36+
return e.index
37+
}
38+
39+
// Err returns the original error that was returned or panicked by the function.
40+
func (e *executionError) Err() error {
41+
return e.err
42+
}
43+
44+
// Error returns the execution error message.
45+
func (e *executionError) Error() string {
46+
return fmt.Sprintf("function %d error: %s", e.index, e.err)
47+
}

error_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 TestExecutionError(t *testing.T) {
11+
a := assert.New(t)
12+
13+
innerErr := errors.New("inner error")
14+
err := &executionError{
15+
index: 0,
16+
err: innerErr,
17+
}
18+
19+
a.EqualNow(err.Error(), "function 0 error: inner error")
20+
a.EqualNow(err.Index(), 0)
21+
a.EqualNow(err.Err(), innerErr)
22+
}

0 commit comments

Comments
 (0)