2021-11-13 11:29:08 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"codeberg.org/gruf/go-bytes"
|
|
|
|
"codeberg.org/gruf/go-logger"
|
|
|
|
)
|
|
|
|
|
2021-11-27 14:26:58 +00:00
|
|
|
// global logfmt data formatter.
|
|
|
|
var logfmt = logger.TextFormat{
|
|
|
|
Strict: logger.DefaultTextFormat.Strict,
|
|
|
|
MaxDepth: logger.DefaultTextFormat.MaxDepth,
|
|
|
|
Levels: nil,
|
|
|
|
TimeFormat: logger.DefaultTextFormat.TimeFormat,
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
|
2021-11-27 14:26:58 +00:00
|
|
|
// KV is a structure for setting key-value pairs in ErrorData.
|
2021-11-13 11:29:08 +00:00
|
|
|
type KV struct {
|
|
|
|
Key string
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorData defines a way to set and access contextual error data.
|
2021-11-27 14:26:58 +00:00
|
|
|
// The default implementation of this is thread-safe.
|
2021-11-13 11:29:08 +00:00
|
|
|
type ErrorData interface {
|
|
|
|
// Value will attempt to fetch value for given key in ErrorData
|
|
|
|
Value(string) (interface{}, bool)
|
|
|
|
|
|
|
|
// Append adds the supplied key-values to ErrorData, similar keys DO overwrite
|
|
|
|
Append(...KV)
|
|
|
|
|
|
|
|
// String returns a string representation of the ErrorData
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:26:58 +00:00
|
|
|
// NewData returns a new ErrorData implementation.
|
2021-11-13 11:29:08 +00:00
|
|
|
func NewData() ErrorData {
|
|
|
|
return &errorData{
|
|
|
|
data: make(map[string]interface{}, 10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorData is our ErrorData implementation, this is essentially
|
2021-11-27 14:26:58 +00:00
|
|
|
// just a thread-safe string-interface map implementation.
|
2021-11-13 11:29:08 +00:00
|
|
|
type errorData struct {
|
|
|
|
data map[string]interface{}
|
|
|
|
buf bytes.Buffer
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *errorData) Value(key string) (interface{}, bool) {
|
|
|
|
d.mu.Lock()
|
|
|
|
v, ok := d.data[key]
|
|
|
|
d.mu.Unlock()
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *errorData) Append(kvs ...KV) {
|
|
|
|
d.mu.Lock()
|
|
|
|
for i := range kvs {
|
|
|
|
k := kvs[i].Key
|
|
|
|
v := kvs[i].Value
|
|
|
|
d.data[k] = v
|
|
|
|
}
|
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *errorData) String() string {
|
|
|
|
d.mu.Lock()
|
|
|
|
|
|
|
|
d.buf.Reset()
|
|
|
|
d.buf.B = append(d.buf.B, '{')
|
|
|
|
logfmt.AppendFields(&d.buf, d.data)
|
|
|
|
d.buf.B = append(d.buf.B, '}')
|
|
|
|
|
|
|
|
d.mu.Unlock()
|
|
|
|
return d.buf.StringPtr()
|
|
|
|
}
|