-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
153 lines (134 loc) · 2.71 KB
/
errors.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package errors
import (
"errors"
"fmt"
"maps"
"slices"
)
type Tag string
type T = Tag
type Label struct {
Key string
Value any
}
func L(key string, value any) Label {
return Label{Key: key, Value: value}
}
func LabelOf(key string, value any) Label {
return Label{Key: key, Value: value}
}
type ErrorWithMeta interface {
error
WithMeta(meta ...any) ErrorWithMeta
}
func New(message string) ErrorWithMeta {
return withMetadata(errors.New(message))
}
func Errorf(format string, args ...any) ErrorWithMeta {
return withMetadata(fmt.Errorf(format, args...))
}
func withMetadata(e error, metadata ...any) ErrorWithMeta {
var tags []Tag
var labels map[string]any
for _, v := range metadata {
if v != nil {
switch m := v.(type) {
case Tag:
tags = append(tags, m)
case Label:
if labels == nil {
labels = make(map[string]any)
}
labels[m.Key] = m.Value
default:
panic(fmt.Errorf("unknown type for metadata: %T", m))
}
}
}
return &errorWrapperWithMetadata{e: e, tags: tags, labels: labels}
}
type errorWrapperWithMetadata struct {
e error
tags []Tag
labels map[string]any
}
func (e *errorWrapperWithMetadata) WithMeta(meta ...any) ErrorWithMeta {
for _, v := range meta {
if v != nil {
switch m := v.(type) {
case Tag:
e.tags = append(e.tags, m)
case Label:
if e.labels == nil {
e.labels = make(map[string]any)
}
e.labels[m.Key] = m.Value
default:
panic(fmt.Errorf("unknown type for metadata: %T", m))
}
}
}
return e
}
func (e *errorWrapperWithMetadata) Error() string {
return e.e.Error()
}
func (e *errorWrapperWithMetadata) Unwrap() error {
return e.e
}
func (e *errorWrapperWithMetadata) Tags() []Tag {
return e.tags
}
func (e *errorWrapperWithMetadata) Labels() map[string]any {
return e.labels
}
func HasTag(e error, tag Tag) bool {
for e != nil {
var em *errorWrapperWithMetadata
if errors.As(e, &em) {
if slices.Contains(em.tags, tag) {
return true
}
}
e = errors.Unwrap(e)
}
return false
}
func HasLabel(e error, key string) bool {
for e != nil {
var em *errorWrapperWithMetadata
if errors.As(e, &em) {
if _, ok := em.labels[key]; ok {
return true
}
}
e = errors.Unwrap(e)
}
return false
}
func GetLabel(e error, key string) any {
for e != nil {
var em *errorWrapperWithMetadata
if errors.As(e, &em) {
if v, ok := em.labels[key]; ok {
return v
}
}
e = errors.Unwrap(e)
}
return nil
}
func GetLabels(e error) map[string]any {
var labels map[string]any
for e != nil {
var em *errorWrapperWithMetadata
if errors.As(e, &em) {
if labels == nil {
labels = make(map[string]any)
}
maps.Copy(labels, em.labels)
}
e = errors.Unwrap(e)
}
return labels
}