kverrors

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2022 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

Package kverrors provides structured errors

errors can be used with pkg/log to further enhance structured logging

Index

Constants

View Source
const (
	MessageKey string = "msg"
	CauseKey   string = "cause"
)

Keys used to log specific builtin fields

Variables

This section is empty.

Functions

func Add

func Add(err error, keyValuePairs ...interface{}) error

Add adds key/value pairs to an error and returns the error WARNING: The original error is modified with this operation

func AddCtx

func AddCtx(err error, ctx Context) error

AddCtx appends Context to the error

func KVSlice

func KVSlice(err error) []interface{}

KVSlice returns the key/value pairs associated with this error as a slice

func KVs

func KVs(err error) map[string]interface{}

KVs returns the key/value pairs associated with this error if it is a *KVError

func Message

func Message(err error) string

Message returns the raw error message

func New

func New(msg string, keysAndValues ...interface{}) error

New creates a new KVError with keys and values

func NewCtx

func NewCtx(msg string, ctx Context, keysAndValues ...interface{}) error

NewCtx creates a new error with Context

func Root

func Root(err error) error

Root unwraps the error until it reaches the root error

func Unwrap

func Unwrap(err error) error

Unwrap provides compatibility with the standard errors package

func Wrap

func Wrap(err error, msg string, keysAndValues ...interface{}) error

Wrap wraps an error as a new error with keys and values

Types

type Context

type Context []interface{}

Context is keyValuePairs wrapped to use later. Usage of this directly is not necessary See Context for more information

func NewContext

func NewContext(keysAndValues ...interface{}) Context

NewContext creates key/value pairs to be used with errors later in the callstack. This provides the ability to create contextual information that will be used with any returned error

Example:

errCtx := kverrors.Context("cluster", clusterName,
    "namespace", namespace)

...

if err != nil {
    return kverrors.Wrap(err, "failed to get namespace").Ctx(errCtx)
}

...

if err != nil {
    return kverrors.Wrap(err, "failed to update cluster").Ctx(errCtx)
}

func (Context) New

func (c Context) New(msg string, keysAndValues ...interface{}) error

New creates a new KVError with this context

func (Context) Wrap

func (c Context) Wrap(err error, msg string, keysAndValues ...interface{}) error

Wrap wraps an error with this context

type KVError

type KVError struct {
	// contains filtered or unexported fields
}

KVError is an error that contains structured keys and values

func (*KVError) Error

func (e *KVError) Error() string

Error returns the string formatted error message. This is required to function as a standard library error

func (*KVError) MarshalJSON

func (e *KVError) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*KVError) Unwrap

func (e *KVError) Unwrap() error

Unwrap returns the error that caused this error. This is required to work with the standard library errors.Unwrap

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL