-
Notifications
You must be signed in to change notification settings - Fork 31
/
trait.go
73 lines (59 loc) · 2.15 KB
/
trait.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
package errorx
// Trait is a static characteristic of an error type.
// All errors of a specific type possess exactly the same traits.
// Traits are both defined along with an error and inherited from a supertype and a namespace.
type Trait struct {
id uint64
label string
}
// RegisterTrait declares a new distinct traits.
// Traits are matched exactly, distinct traits are considered separate event if they have the same label.
func RegisterTrait(label string) Trait {
return newTrait(label)
}
// HasTrait checks if an error possesses the expected trait.
// Traits are always properties of a type rather than of an instance, so trait check is an alternative to a type check.
// This alternative is preferable, though, as it is less brittle and generally creates less of a dependency.
func HasTrait(err error, key Trait) bool {
typedErr := Cast(err)
if typedErr == nil {
return false
}
return typedErr.HasTrait(key)
}
// Temporary is a trait that signifies that an error is temporary in nature.
func Temporary() Trait { return traitTemporary }
// Timeout is a trait that signifies that an error is some sort iof timeout.
func Timeout() Trait { return traitTimeout }
// NotFound is a trait that marks such an error where the requested object is not found.
func NotFound() Trait { return traitNotFound }
// Duplicate is a trait that marks such an error where an update is failed as a duplicate.
func Duplicate() Trait { return traitDuplicate }
// IsTemporary checks for Temporary trait.
func IsTemporary(err error) bool {
return HasTrait(err, Temporary())
}
// IsTimeout checks for Timeout trait.
func IsTimeout(err error) bool {
return HasTrait(err, Timeout())
}
// IsNotFound checks for NotFound trait.
func IsNotFound(err error) bool {
return HasTrait(err, NotFound())
}
// IsDuplicate checks for Duplicate trait.
func IsDuplicate(err error) bool {
return HasTrait(err, Duplicate())
}
var (
traitTemporary = RegisterTrait("temporary")
traitTimeout = RegisterTrait("timeout")
traitNotFound = RegisterTrait("not_found")
traitDuplicate = RegisterTrait("duplicate")
)
func newTrait(label string) Trait {
return Trait{
id: nextInternalID(),
label: label,
}
}