Documentation
¶
Index ¶
- func As(err error, target any) bool
- func Disjoin(err error) []error
- func Fields(err error) []any
- func Is(err, target error) bool
- func Join(opts ...any) error
- func New(msg string, opts ...any) error
- func Unwrap(err error) error
- func V(err error, key string) any
- func Wrap(err error, opts ...any) error
- type Frame
- type FrameSkips
- type JoinFormatFn
- type KV
- type Kind
- type StackFrames
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As is a direct callout to the std lib errors.As function. This allows users to only ever have to worry about including one errors pkg.
func Is ¶
Is is a direct callout to the std lib errors.Is function. This allows users to only ever have to worry about including one errors pkg.
func Join ¶
Join returns a new multi error.
TODO:
- play with Join(opts ...any) and Join(errs []error, opts ...any) sigs and ask for feedback regarding tradeoffs with type safety of first arg. As of writing some tests, I kind of dig the loose Join(opts ...any).
func Unwrap ¶
Unwrap is a direct callout to the std lib errors.Unwrap function. This allows users to only ever have to worry about including one errors pkg. The use of the std lib errors.Unwrap is a now thing, but may change in future releases.
func V ¶
V returns a typed value for the kvs of an error. Type conversion can be used to convert the output value. We do not distinguish between a purposeful <nil> value and key not found. With the single return param, we can do the following to convert it to a more specific type:
err := errors.New("simple msg", errors.KVs("int", 1))
i, ok := errors.V(err, "int).(int)
Note: this will take the first matching key. If you are interested in obtaining a key's value from a wrapped error collides with a parent's key value, then you can manually unwrap the error and call V on it to skip the parent field.
TODO:
- food for thought, we could change the V funcs signature to allow for a generic type to be provided, however... it feels both premature and limiting in the event you don't care about the type. If we get an ask for that, we can provide guidance for this via the comment above and perhaps some example code.
Types ¶
type Frame ¶
Frame is a single step in stack trace.
func (Frame) Format ¶
Format formats the frame according to the fmt.Formatter interface.
%s source file %d source line %n function name %v equivalent to %s:%d
Format accepts flags that alter the printing of some verbs, as follows:
%+s function name and path of source file relative to the compile time
GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v equivalent to %+s:%d
type FrameSkips ¶
type FrameSkips int
FrameSkips marks the number of frames to skip in collecting the stack frame. This is helpful when creating helper functions. TODO(berg): give example of helper functions here
const ( // NoFrame marks the error to not have an error frame captured. This is useful // when the stack frame is of no use to you the consumer. NoFrame FrameSkips = -1 // SkipCaller skips the immediate caller of the functions. Useful for creating // reusable Error constructors in consumer code. SkipCaller FrameSkips = 1 )
type JoinFormatFn ¶
JoinFormatFn is the join errors formatter. This allows the user to customize the text output when calling Error() on the join error.
type KV ¶
KV provides context to the error. These can be triggered by different formatter options with fmt.*printf calls of the error. TODO:
- explore other data structures for passing in the key val pairs
func KVs ¶
KVs takes a slice of argument kv pairs, where the first of each pair must be the key string, and the latter the value. Additionally, a key that is a type that implements the strings.Stringer interface is also accepted.
TODO:
- I really like the ergonomics of this when working with errors, quite handy to replace the exhaustion of working with the KV type above. similar approach maybe useful for other sorts of metadata as well.
type Kind ¶
type Kind string
Kind represents the category of the error type. A few examples of error kinds are as follows:
const (
// represents an error for a entity/thing that was not found
errKindNotFound = errors.Kind("not found")
//represents an error for a validation error
errKindInvalid = errors.Kind("invalid")
)
With the kind, you can write common error handling across the error's Kind. This can create a dramatic improvement abstracting errors, allowing the behavior (kind) of an error to dictate semantics instead of having to rely on N sentinel or custom error types.
Additionally, the Kind can be used to assert that an error is of a kind. The following uses the std lib errors.Is to determine if the target error is of kind "first":
err := errors.New("some error", errors.Kind("first"))
errors.Is(err, errors.Kind("first")) // output is true
type StackFrames ¶
type StackFrames []Frame
StackFrames represents a slice of stack Frames in LIFO order (follows path of code to get the original error). TODO:
- add String method for this slice of frames so it can be used without fuss in logging
- add Formatter to be able to turn off the way it prints
func StackTrace ¶
func StackTrace(err error) StackFrames
StackTrace returns the StackFrames for an error. See StackFrames for more info. TODO:
- make this more robust with Is
- determine if its even worth exposing an accessor for this private method
- allow for StackTraces() to accommodate joined errors, perhaps returning a map[string]StackFrames or some graph representation would be awesome.