-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
80 lines (69 loc) · 1.84 KB
/
error.go
File metadata and controls
80 lines (69 loc) · 1.84 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package task
import (
"context"
"errors"
)
// HandleErr creates a task that handles specific error after running t.
// It could change the error returned by Run. f is called only if t.Run returns an
// error.
func (t Task) HandleErr(f func(error) error) Task {
return func(ctx context.Context) error {
err := t.Run(ctx)
if err != nil {
err = f(err)
}
return err
}
}
// HandleErrWithContext is like HandleErr, but uses same context used in f.
func (t Task) HandleErrWithContext(f func(context.Context, error) error) Task {
return func(ctx context.Context) error {
err := t.Run(ctx)
if err != nil {
err = f(ctx, err)
}
return err
}
}
// ContextError detects if err is [context.Canceled] or [context.DeadlineExceeded].
func ContextError(err error) bool {
return ErrorIs(context.Canceled, context.DeadlineExceeded)(err)
}
// ErrorIs creates a function to detect if the error is listed.
func ErrorIs(errs ...error) func(error) bool {
return func(err error) bool {
for _, e := range errs {
if errors.Is(err, e) {
return true
}
}
return false
}
}
// OnlyErrs preserves errors if f(error) is true.
func (t Task) OnlyErrs(f func(error) bool) Task {
return t.HandleErr(func(err error) error {
if f(err) {
return err
}
return nil
})
}
// IgnoreErrs ignores errors if f(error) is true.
func (t Task) IgnoreErrs(f func(error) bool) Task {
return t.HandleErr(func(err error) error {
if f(err) {
return nil
}
return err
})
}
// IgnoreErr ignores the error returned by t.Run if it is not context error.
//
// It is shortcut to t.OnlyErrs(ContextError).
func (t Task) IgnoreErr() Task {
return t.OnlyErrs(ContextError)
}