-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.go
87 lines (71 loc) · 2.51 KB
/
classifier.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package sagas
import "errors"
// Classifier is the interface to classify errors. It is used to determine
// whether an action should be retried or not.
type Classifier interface {
// Classify receives an error and returns a Status. If the error is nil, it
// returns Successed; if the error is not nil, it returns Retry.
Classify(error) Status
}
type classifier struct{}
// NewClassifier creates a new default classifier. It is the default
// classifier used if no classifier is provided. If the error is nil, it
// returns Successed, otherwise it returns Retry. Example:
//
// classifier := sagas.NewClassifier()
//
// The above example will create a new default classifier that will return Retry if the error is not nil.
func NewClassifier() Classifier {
return classifier{}
}
// Classify implements the classifier interface for the default classifier.
func (c classifier) Classify(err error) Status {
if err == nil {
return Successed
}
return retry
}
type classifierWhitelist []error
// NewClassifierWhitelist creates a new whitelist classifier. If the error is nil, it
// returns Successed; if the error is in the whitelist, it returns Retry; otherwise, it returns Failed. Example:
//
// classifier := sagas.NewClassifierWhitelist(errors.New("error"))
//
// The above example will create a new whitelist classifier that will return Retry if the error is errors.New("error").
func NewClassifierWhitelist(errors ...error) Classifier {
return classifierWhitelist(errors)
}
// Classify implements the classifier interface for the whitelist classifier.
func (list classifierWhitelist) Classify(err error) Status {
if err == nil {
return Successed
}
for _, pass := range list {
if errors.Is(err, pass) {
return retry
}
}
return Failed
}
type classifierBlacklist []error
// NewClassifierBlacklist creates a new blacklist classifier. If the error is nil, it
// returns Successed; if the error is in the blacklist, it returns Failed; otherwise, it returns Retry. Example:
//
// classifier := sagas.NewClassifierBlacklist(errors.New("error"))
//
// The above example will create a new blacklist classifier that will return Failed if the error is errors.New("error").
func NewClassifierBlacklist(errors ...error) Classifier {
return classifierBlacklist(errors)
}
// Classify implements the classifier interface for the blacklist classifier.
func (list classifierBlacklist) Classify(err error) Status {
if err == nil {
return Successed
}
for _, pass := range list {
if errors.Is(err, pass) {
return Failed
}
}
return retry
}