errors

package
v1.127.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 4 Imported by: 6

Documentation

Overview

Package errors provides error handling with lazy stack trace support. Drop-in replacement for github.com/pkg/errors using only standard library.

Stack traces are captured lazily: - Fast path: Only capture program counters ([]uintptr) when error is created - Slow path: Resolve to human-readable stack only when %+v is used

Usage:

import "github.com/donnyhardyanto/dxlib/errors"

err := errors.New("something failed")
err := errors.Wrap(err, "context")
fmt.Printf("%+v\n", err)  // prints with stack trace

Index

Constants

This section is empty.

Variables

View Source
var (
	Is     = stderrors.Is
	As     = stderrors.As
	Unwrap = stderrors.Unwrap
	Join   = stderrors.Join
)

Functions

func Cause

func Cause(err error) error

Cause returns the root cause of error by unwrapping all layers.

func Errorf

func Errorf(format string, args ...any) error

Errorf creates formatted error with stack trace.

func New

func New(message string) error

New creates error with message and captures stack trace. Stack is captured as raw pointers (fast), resolved only when %+v is used.

func WithMessage

func WithMessage(err error, message string) error

WithMessage wraps error with message but NO new stack trace. Use this when you want to add context without capturing a new stack. Returns nil if err is nil.

func WithMessagef

func WithMessagef(err error, format string, args ...any) error

WithMessagef wraps error with formatted message but NO new stack trace. Returns nil if err is nil.

func WithStack

func WithStack(err error) error

WithStack adds stack trace to existing error without additional message. Returns nil if err is nil.

func Wrap

func Wrap(err error, message string) error

Wrap wraps error with message and captures stack trace. Returns nil if err is nil.

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf wraps error with formatted message and captures stack trace. Returns nil if err is nil.

Types

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

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 StackTrace

type StackTrace []Frame

StackTrace is a stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

Jump to

Keyboard shortcuts

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