skerr

package
v0.0.0-...-b95a7b5 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: BSD-3-Clause Imports: 3 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fmt

func Fmt(fmtStr string, args ...interface{}) error

Fmt is equivalent to Wrap(fmt.Errorf(...)).

func Unwrap

func Unwrap(err error) error

Unwrap returns the original error if err is ErrWithContext, otherwise just returns err.

func Wrap

func Wrap(err error) error

Wrap adds stack trace info to err, if not already present. The return value will be of type ErrorWithContext. If err is nil, nil is returned instead.

func Wrapf

func Wrapf(err error, fmtStr string, args ...interface{}) error

Wrapf adds context and stack trace info to err. Existing stack trace info will be preserved. The return value will be of type ErrorWithContext. Example: sklog.Wrapf(err, "When loading %d items from %s", count, url)

If err is nil, nil is returned instead.

Types

type ErrorWithContext

type ErrorWithContext struct {
	// Wrapped is the original error. Never nil.
	Wrapped error
	// CallStack captures the caller's stack at the time the ErrorWithContext was created; most recent
	// call first.
	CallStack []StackTrace
	// Context contains additional info from calls to Wrapf. The slice is in order of calls to Wrapf,
	// which Error() prints in reverse.
	Context []string
}

ErrorWithContext contains an original error with context info and a stack trace. It implements the error interface.

func (*ErrorWithContext) Error

func (err *ErrorWithContext) Error() string

func (*ErrorWithContext) Unwrap

func (err *ErrorWithContext) Unwrap() error

Unwrap allows unwrapping an error as implemented in the `errors` standard library.

type StackTrace

type StackTrace struct {
	File string
	Line int
}

StackTrace identifies a filename (base filename only) and line number.

func CallStack

func CallStack(height, startAt int) []StackTrace

CallStack returns a slice of StackTrace representing the current stack trace. The lines returned start at the depth specified by startAt: 0 means the call to CallStack, 1 means CallStack's caller, 2 means CallStack's caller's caller and so on. height means how many lines to include, counting deeper into the stack, with zero meaning to include all stack frames. If height is non-zero and there aren't enough stack frames, a placeholder value is used instead.

Suppose the stacktrace looks like: skerr.go:300 <- the call to runtime.Caller in skerr.CallStack alpha.go:123 beta.go:456 gamma.go:789 delta.go:123 main.go: 70 A typical call may look like skerr.CallStack(6, 1), which returns [{File:alpha.go, Line:123}, {File:beta.go, Line:456},...,

{File:main.go, Line:70}, {File:???, Line:1}], omitting the not-helpful reference to

CallStack and padding the response with a placeholder value, since the stack was not tall enough to show 6 items, starting at the second one.

func (*StackTrace) String

func (st *StackTrace) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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