-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmanager.go
58 lines (45 loc) · 1.26 KB
/
manager.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
package trm
//go:generate mockgen -source=$GOFILE -destination=drivers/mock/$GOFILE -package=mock
import (
"context"
"errors"
"go.uber.org/multierr"
)
// Manager manages a transaction from Begin to Commit or Rollback.
type Manager interface {
// Do processes a transaction inside a closure.
Do(context.Context, func(ctx context.Context) error) error
// DoWithSettings processes a transaction inside a closure with custom trm.Settings.
DoWithSettings(context.Context, Settings, func(ctx context.Context) error) error
}
// ErrSkip marks error to skip rollback for transaction because of inside error.
var ErrSkip = errors.New("skippable")
// Skippable marks error as ErrSkip.
func Skippable(err error) error {
if err == nil {
return nil
}
return multierr.Append(err, ErrSkip)
}
// UnSkippable removes ErrSkip from error.
func UnSkippable(err error) error {
if err == nil || !IsSkippable(err) {
return err
}
ee := multierr.Errors(err)
res := make([]error, 0, len(ee))
for _, e := range ee {
//nolint:errorlint,goerr113
if e != ErrSkip {
res = append(res, e)
}
}
return multierr.Combine(res...)
}
// IsSkippable checks that the error is ErrSkip.
func IsSkippable(err error) bool {
if err == nil {
return false
}
return errors.Is(err, ErrSkip)
}