Documentation ¶
Index ¶
- Constants
- func Add(ctx context.Context, kvs ...any) context.Context
- func AddLabelCounter(ctx context.Context, counter Adder) context.Context
- func AddMap[K comparable, V any](ctx context.Context, m map[K]V) context.Context
- func AddMapTo[K comparable, V any](ctx context.Context, namespace string, m map[K]V) context.Context
- func AddTo(ctx context.Context, namespace string, kvs ...any) context.Context
- func AddTrace(ctx context.Context) context.Context
- func AddTraceName(ctx context.Context, name string, kvs ...any) context.Context
- func AddTraceNameTo(ctx context.Context, name, namespace string, kvs ...any) context.Context
- func AddTraceTo(ctx context.Context, namespace string) context.Context
- func Conceal(a any) string
- func ConcealWith(alg hashAlg, s string) string
- func HasLabel(err error, label string) bool
- func Hide(a any) secret
- func HideAll(a ...any) []secret
- func In(ctx context.Context) *dataNode
- func InErr(err error) *dataNode
- func InNamespace(ctx context.Context, namespace string) *dataNode
- func Labels(err error) map[string]struct{}
- func Mask(a any) secret
- func SetHasher(sc HashCfg)
- func Unwrap(err error) error
- type Adder
- type Concealer
- type Err
- func Label(err error, label string) *Err
- func New(msg string) *Err
- func NewWC(ctx context.Context, msg string) *Err
- func Stack(errs ...error) *Err
- func StackWC(ctx context.Context, errs ...error) *Err
- func With(err error, kvs ...any) *Err
- func WithClues(err error, ctx context.Context) *Err
- func WithMap(err error, m map[string]any) *Err
- func WithTrace(err error, depth int) *Err
- func Wrap(err error, msg string) *Err
- func WrapWC(ctx context.Context, err error, msg string) *Err
- func (err *Err) As(target any) bool
- func (err *Err) Core() *ErrCore
- func (err *Err) Error() string
- func (err *Err) Format(s fmt.State, verb rune)
- func (err *Err) HasLabel(label string) bool
- func (err *Err) Is(target error) bool
- func (err *Err) Label(labels ...string) *Err
- func (err *Err) Labels() map[string]struct{}
- func (err *Err) OrNil() error
- func (err *Err) Unwrap() error
- func (err *Err) Values() *dataNode
- func (err *Err) With(kvs ...any) *Err
- func (err *Err) WithClues(ctx context.Context) *Err
- func (err *Err) WithMap(m map[string]any) *Err
- func (err *Err) WithTrace(depth int) *Err
- type ErrCore
- type HashCfg
Constants ¶
const ( SHA256 hashAlg = iota HMAC_SHA256 Plaintext Flatmask )
Variables ¶
This section is empty.
Functions ¶
func AddLabelCounter ¶
AddLabelCounter embeds an Adder interface into this context. Any already embedded Adder will get replaced. When adding Labels to a clues.Err the LabelCounter will use the label as the key for the Add call, and increment the count of that label by one.
func AddMapTo ¶
func AddMapTo[K comparable, V any](ctx context.Context, namespace string, m map[K]V) context.Context
AddMapTo adds a shallow clone of the map to a namespaced set of clues.
func AddTrace ¶
AddTrace stacks a clues node onto this context. Adding a node ensures that this point in code is identified by an ID, which can later be used to correlate and isolate logs to certain trace branches. AddTrace is only needed for layers that don't otherwise call Add() or similar functions, since those funcs already attach a new node.
func AddTraceName ¶
AddTraceName stacks a clues node onto this context and uses the provided name for the trace id, instead of a randomly generated hash. AddTraceName can be called without additional values if you only want to add a trace marker.
func AddTraceNameTo ¶
AddTraceNameTo stacks a clues node onto this context and uses the provided name for the trace id, instead of a randomly generated hash. AddTraceNameTo can be called without additional values if you only want to add a trace marker.
func AddTraceTo ¶
AddTraceTo stacks a clues node onto this context within the specified namespace. Adding a node ensures that a point in code is identified by an ID, which can later be used to correlate and isolate logs to certain trace branches. AddTraceTo is only needed for layers that don't otherwise call AddTo() or similar functions, since those funcs already attach a new node.
func ConcealWith ¶
Conceal runs one of clues' hashing algorithms on the provided string.
func Hide ¶
func Hide(a any) secret
Hide embeds the value in a secret struct where the Conceal() call contains a truncated hash of value. The hash function defaults to SHA256, but can be changed through configuration.
func HideAll ¶
func HideAll(a ...any) []secret
HideAll is a quality-of-life wrapper for transforming multiple values to secret structs.
func InErr ¶
func InErr(err error) *dataNode
InErr returns the map of contextual values in the error. Each error in the stack is unwrapped and all maps are unioned. In case of collision, lower level error data take least priority.
func InNamespace ¶
InNamespace returns the map of values in the given namespace.
func Mask ¶
func Mask(a any) secret
Mask embeds the value in a secret struct where the Conceal() call always returns a flat string: "***"
Types ¶
type Concealer ¶
type Concealer interface { Conceal() string // Concealers also need to comply with Format // It's a bit overbearing, but complying with Concealer // doesn't provide guarantees that the variable won't // pass into fmt.Printf("%v") and skip the whole hash. // This is for your protection, too. Format(fs fmt.State, verb rune) // PlainStringer is the opposite of conceal. // Useful for if you want to retrieve the raw value of a secret. PlainString() string }
type Err ¶
type Err struct {
// contains filtered or unexported fields
}
Err augments an error with labels (a categorization system) and data (a map of contextual data used to record the state of the process at the time the error occurred, primarily for use in upstream logging and other telemetry),
func Stack ¶
Stack returns the error as a clues.Err. If additional errors are provided, the entire stack is flattened and returned as a single error chain. All messages and stored structure is aggregated into the returned err.
Ex: Stack(sentinel, errors.New("base")).Error() => "sentinel: base"
func With ¶
With adds every two values as a key,value pair to the Err's data map. If err is not an *Err intance, returns the error wrapped into an *Err struct.
func WithClues ¶
WithClues is syntactical-sugar that assumes you're using the clues package to store structured data in the context. The values in the default namespace are retrieved and added to the error.
clues.WithClues(err, ctx) adds the same data as clues.WithMap(err, clues.Values(ctx)).
If the context contains a clues LabelCounter, that counter is passed to the error. WithClues must always be called first in order to count labels.
func WithMap ¶
WithMap copies the map to the Err's data map. If err is not an *Err intance, returns the error wrapped into an *Err struct.
func WithTrace ¶
WithTrace sets the error trace to a certain depth. A depth of 0 traces to the func where WithTrace is called. 1 sets the trace to its parent, etc. Error traces are already generated for the location where clues.Wrap or clues.Stack was called. This call is for cases where Wrap or Stack calls are handled in a helper func and are not reporting the actual error origin. If err is not an *Err intance, returns the error wrapped into an *Err struct.
func WrapWC ¶
WrapWC is equivalent to clues.Wrap(err, "msg").WithClues(ctx) Wrap returns a clues.Err with a new message wrapping the old error.
func (*Err) As ¶
As overrides the standard As check for Err.e, allowing us to check the conditional for both Err.e and Err.next. This allows clues to Stack() maintain multiple error pointers without failing the otherwise linear errors.As check.
func (*Err) Core ¶
Core transforms the Err to an ErrCore, flattening all the errors in the stack into a single struct.
func (*Err) Format ¶
Format ensures stack traces are printed appropariately.
%s same as err.Error() %v equivalent to %s
Format accepts flags that alter the printing of some verbs, as follows:
%+v Prints filename, function, and line number for each error in the stack.
func (*Err) Is ¶
Is overrides the standard Is check for Err.e, allowing us to check the conditional for both Err.e and Err.next. This allows clues to Stack() maintain multiple error pointers without failing the otherwise linear errors.Is check.
func (*Err) OrNil ¶
OrNil is a workaround for golang's infamous "an interface holding a nil value is not nil" gotcha. You can use it at the end of error formatting chains to ensure a correct nil return value.
func (*Err) Unwrap ¶
Unwrap provides compatibility for Go 1.13 error chains. Unwrap returns the Unwrap()ped base error, if it implements the unwrapper interface:
type unwrapper interface { Unwrap() error }
If the error does not implement Unwrap, returns the base error.
func (*Err) Values ¶
func (err *Err) Values() *dataNode
Values returns a copy of all of the contextual data in the error. Each error in the stack is unwrapped and all maps are unioned. In case of collision, lower level error data take least priority.
func (*Err) WithClues ¶
WithClues is syntactical-sugar that assumes you're using the clues package to store structured data in the context. The values in the default namespace are retrieved and added to the error.
clues.Stack(err).WithClues(ctx) adds the same data as clues.Stack(err).WithMap(clues.Values(ctx)).
If the context contains a clues LabelCounter, that counter is passed to the error. WithClues must always be called first in order to count labels.
func (*Err) WithTrace ¶
WithTrace sets the error trace to a certain depth. A depth of 0 traces to the func where WithTrace is called. 1 sets the trace to its parent, etc. Error traces are already generated for the location where clues.Wrap or clues.Stack was called. This call is for cases where Wrap or Stack calls are handled in a helper func and are not reporting the actual error origin.
type ErrCore ¶
type ErrCore struct { Msg string `json:"msg"` Labels map[string]struct{} `json:"labels"` Values map[string]any `json:"values"` }
ErrCore is a minimized version of an Err{}. Primarily intended for serializing a flattened version of the error stack
func ToCore ¶
ToCore transforms the Err to an ErrCore, flattening all the errors in the stack into a single struct
func (*ErrCore) Format ¶
Format provides cleaner printing of an ErrCore struct.
%s only populated values are printed, without printing the property name. %v same as %s.
Format accepts flags that alter the printing of some verbs, as follows:
%+v prints the full struct, including empty values and property names.