-
Notifications
You must be signed in to change notification settings - Fork 300
/
attributes.go
191 lines (171 loc) · 4.56 KB
/
attributes.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errors
import (
"errors"
"fmt"
"reflect"
)
type attributer interface {
Attributes() map[string]any
}
type publicAttributer interface {
PublicAttributes() map[string]any
}
var errOddKV = DefineInvalidArgument("odd_kv", "Odd number of key-value elements")
func supported(v any) any {
if v == nil {
return "<nil>"
}
rv := reflect.Indirect(reflect.ValueOf(v))
switch rv.Type().Kind() {
case reflect.Int:
return int(rv.Int())
case reflect.Float64:
return rv.Float()
case reflect.String:
return rv.String()
default:
return fmt.Sprintf("%v", v)
}
}
func kvToMap(kv ...any) (map[string]any, error) {
if len(kv)%2 != 0 {
return nil, errOddKV.New()
}
m := make(map[string]any, len(kv)/2)
var key string
for i, node := range kv {
if i%2 == 0 {
key = fmt.Sprintf("%v", node)
} else {
m[key] = node
}
}
return m, nil
}
func (e *Error) mergeAttributes(kv ...any) {
if len(kv) == 0 {
return
}
attributes, err := kvToMap(kv...)
if err != nil {
panic(err)
}
if e.attributes != nil {
// Merge with existing attributes:
for k, v := range e.attributes {
if _, ok := attributes[k]; !ok {
attributes[k] = v
}
}
}
e.attributes = attributes
// Ensure that all attributes used as message format arguments are supported:
for k, v := range e.attributes {
for _, arg := range e.messageFormatArguments {
if k == arg {
e.attributes[k] = supported(v)
}
}
}
// Set the call stack if not already set:
if e.stack == nil {
e.stack = callers(4)
}
e.clearGRPCStatus()
}
// WithAttributes returns the error with the given attributes set.
// Any conflicting attributes in the Error will be overwritten.
func (e *Error) WithAttributes(kv ...any) *Error {
if e == nil {
return e
}
deriv := *e
deriv.mergeAttributes(kv...)
return &deriv
}
// WithAttributes returns a new error from the definition, and sets the given attributes.
func (d *Definition) WithAttributes(kv ...any) *Error {
if d == nil {
return nil
}
e := build(d, 0) // Don't refactor this to build(...).WithAttributes(...)
e.mergeAttributes(kv...)
return e
}
// Attributes of the error.
func (e *Error) Attributes() map[string]any {
if e == nil {
return nil
}
return e.attributes
}
// PublicAttributes of the error.
func (e *Error) PublicAttributes() map[string]any {
if e == nil {
return nil
}
if len(e.attributes) == 0 {
return nil
}
publicAttributes := make(map[string]any, len(e.attributes))
nextAttr:
for k, v := range e.attributes {
for _, public := range e.publicAttributes {
if k == public {
publicAttributes[k] = v
continue nextAttr
}
}
}
if len(publicAttributes) == 0 {
return nil
}
return publicAttributes
}
// Attributes are not present in the error definition, so this just returns nil.
func (*Definition) Attributes() map[string]any { return nil }
// PublicAttributes are not present in the error definition, so this just returns nil.
func (*Definition) PublicAttributes() map[string]any { return nil }
// Attributes returns the attributes of the errors, if they implement Attributes().
// If more than one error is passed, subsequent error attributes will be added if not set.
func Attributes(err ...error) map[string]any {
attributes := make(map[string]any)
for _, err := range err {
if attrErr := (attributer)(nil); errors.As(err, &attrErr) {
for k, v := range attrErr.Attributes() {
if _, ok := attributes[k]; !ok {
attributes[k] = v
}
}
}
}
return attributes
}
// PublicAttributes returns the public attributes of the errors, if they implement PublicAttributes().
// If more than one error is passed, subsequent error attributes will be added if not set.
func PublicAttributes(err ...error) map[string]any {
attributes := make(map[string]any)
for _, err := range err {
if attrErr := (publicAttributer)(nil); errors.As(err, &attrErr) {
for k, v := range attrErr.PublicAttributes() {
if _, ok := attributes[k]; !ok {
attributes[k] = v
}
}
}
}
return attributes
}