errors

package
v0.0.0-...-055e339 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TestingConfig = trace.StackConfig{
	TrimRuntime:   true,
	RemoveLambdas: true,
	FormatStack: func(call stack.Call) string {
		return fmt.Sprintf("%s %n", call, call)
	},
	FormatReference: func(call stack.Call) string {
		return fmt.Sprintf("%s %n", call, call)
	},
}

Functions

func As

func As(err error, target any) bool

As is an alias of the standard library's errors.As() function.

func Flatten

func Flatten(err error) [][]error

Flatten walks the error tree, creating a path for each leaf of the tree if the tree looks like this:

 ── a
	└── b
	    ├── c
	    │   └── e
	    │       └── f
	    └── d
	        ├── g
	        └── h

Then the paths we will get are: [a, b, c, e, f] [a, b, d, g] [a, b, d, h]

func GetCodes

func GetCodes(err error) []string

GetCodes returns the stack of error codes in the given jettison error chain. The error codes are returned in reverse-order of calls to Wrap(), i.e. the code of the latest wrapped error comes first in the list.

func GetKeyValues

func GetKeyValues(err error) map[string]string

GetKeyValues returns all embedded key value info in the error

func GetLastStackTrace

func GetLastStackTrace(err error) (string, []string, bool)

func Is

func Is(err, target error) bool

Is is an alias of the standard library's errors.Is() function.

func IsAny

func IsAny(err error, targets ...error) bool

IsAny returns true if Is(err, target) is true for any of the targets.

func Join

func Join(err ...error) error

Join is an alias of the standard library's errors.Join() function.

func New

func New(msg string, ol ...Option) error

New creates a new JettisonError with a populated stack trace

func SetLegacyCallback

func SetLegacyCallback(f func(src, target error))

func SetTraceConfig

func SetTraceConfig(config trace.StackConfig)

func SetTraceConfigTesting

func SetTraceConfigTesting(t testing.TB, config trace.StackConfig)

func Unwrap

func Unwrap(err error) error

Unwrap is an alias of the standard library's errors.Unwrap() function.

func Walk

func Walk(err error, do func(error) bool)

Walk will do a depth first traversal of the error tree. do is called for each error on the traversal, if it returns false, then the traversal will be terminated

func Wrap

func Wrap(err error, msg string, ol ...Option) error

Wrap will wrap an existing error in a new JettisonError. If no error in the err error tree has a trace, a stack trace is populated.

Types

type ErrorOption

type ErrorOption func(je *internal.Error)

func (ErrorOption) ApplyToError

func (o ErrorOption) ApplyToError(je *internal.Error)

type Option

type Option interface {
	ApplyToError(je *internal.Error)
}

func C

func C(code string) Option

func WithCode

func WithCode(code string) Option

WithCode sets an error code on the error. A code should uniquely identity an error, the intention being to provide an equality check for jettison errors (see Is() for more details). The default code (the error message) doesn't provide strong unique guarantees.

func WithStackTrace

func WithStackTrace() Option

WithStackTrace will add a new stack trace to this error

func WithoutStackTrace

func WithoutStackTrace() Option

WithoutStackTrace clears any automatically populated stack trace. New always populates a stack trace and Wrap will if no sub error has a trace.

This Option is useful for sentinel errors which have a useless init-time stack trace. Removing it allows a stacktrace to be added when it is Wrapped.

Example

var ErrFoo = errors.New("foo", errors.WithoutStackTrace()) // Clear useless init-time stack trace.

func bar() error {
  return errors.Wrap(ErrFoo, "bar") // Wrapping ErrFoo adds a proper stack trace.
}

Jump to

Keyboard shortcuts

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