stackerr

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf

func Errorf(format string, args ...interface{}) error

Errorf with stack trace from caller. Always use %w to wrap errors

example:

return stackerr.Errorf("%w", err)

Then elsewhere,

> log.Printf("Error: %+v", err)

func Wrap

func Wrap(err error) error

Wrap with stack trace from caller's caller (nil error returns nil)

See WrapN to use additional caller skip

Types

type FuncCallerInfo

type FuncCallerInfo struct {
	Name string    `json:"name,omitempty"`
	File string    `json:"file,omitempty"`
	Line int       `json:"line,omitempty"`
	Time time.Time `json:"time,omitempty"`
}

func GetFuncCallerInfo

func GetFuncCallerInfo(skips ...int) FuncCallerInfo

func (FuncCallerInfo) String

func (fci FuncCallerInfo) String() string

type StackError

type StackError struct {
	St FuncCallerInfo `json:"caller"`
	// contains filtered or unexported fields
}

StackError wraps an error and holds the stack trace St (FuncCallerInfo)

Create with Errorf or Wrap (`stackerr.Errorf("%w", err)`) Print with `%+v` verb (or call err.Stack()) Use err.Unwrap() (or errors.Unwrap(err)) to retrieve underlying error

Example:

err := foo()
if err != nil {
  return fmt.Errorf("%w", err)
}

in main, `log.Fatalf("%+v", err)`

func M added in v0.1.2

func M(err error, st FuncCallerInfo) *StackError

M do not use

func WrapN added in v0.1.2

func WrapN(err error, skip int) *StackError

WrapN with stack trace from caller's caller (nil error returns nil)

func (*StackError) Format

func (s *StackError) Format(f fmt.State, c rune)

Format implements fmt.Formatter

%+v for nice stack trace, %+z for single-line version suitable for slog package

func (*StackError) MarshalText added in v0.1.2

func (s *StackError) MarshalText() ([]byte, error)

MarshalText for getting slog package to print %+z mode (no newlines)

func (*StackError) Stack

func (s *StackError) Stack() FuncCallerInfo

func (*StackError) Underlying

func (s *StackError) Underlying() error

func (*StackError) Unwrap

func (s *StackError) Unwrap() error

Jump to

Keyboard shortcuts

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