errutil

package
v1.20.6 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package errutil provides utilities for error handling and manipulation.

This package offers error boxing, stack traces, and panic recovery utilities to enhance error handling capabilities in Go applications.

Examples

// Box any error type
errBox := errutil.BoxErr(errors.New("something went wrong"))
fmt.Println(errBox.Error())

// Recover from panic with stack trace
defer errutil.Recover(func(err error) {
	fmt.Printf("Panic recovered: %v\n", err)
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrBox

type ErrBox struct {
	// contains filtered or unexported fields
}

ErrBox is a wrapper for any error type. Use ToError() method to convert to error interface, or access ErrBox through Result.Err().

func BoxErr

func BoxErr(val any) ErrBox

BoxErr wraps any error type into ErrBox. Returns zero value ErrBox{} if val is nil.

func (ErrBox) As

func (e ErrBox) As(target any) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true. Otherwise, it returns false.

func (ErrBox) GoString

func (e ErrBox) GoString() string

GoString returns the Go-syntax representation. This implements the fmt.GoStringer interface.

func (ErrBox) Is

func (e ErrBox) Is(target error) bool

Is reports whether any error in err's chain matches target.

func (ErrBox) IsEmpty

func (e ErrBox) IsEmpty() bool

IsEmpty returns true if ErrBox is empty (nil val).

func (ErrBox) String

func (e ErrBox) String() string

String returns the string representation. This implements the fmt.Stringer interface.

func (ErrBox) ToError

func (e ErrBox) ToError() error

ToError converts ErrBox to error interface. Returns nil if val is nil. Returns the wrapped error directly if it's already an error, otherwise returns an innerErrBox value which implements the error interface.

Example:

```go
var eb errutil.ErrBox = errutil.BoxErr(errors.New("test"))
var err error = eb.ToError() // err is the original error
```

func (ErrBox) Unwrap

func (e ErrBox) Unwrap() error

Unwrap returns the inner error.

func (ErrBox) Value

func (e ErrBox) Value() any

Value returns the inner value.

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

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type PanicError

type PanicError struct {
	// contains filtered or unexported fields
}

PanicError wraps an error with its panic stack trace. This is used by Catch() to preserve stack trace information when converting panics to errors.

func NewPanicError

func NewPanicError(err any, stack StackTrace) *PanicError

NewPanicError creates a new PanicError with the given error and stack trace.

func (*PanicError) Error

func (e *PanicError) Error() string

Error returns the error message without stack trace information. To include stack trace, use fmt.Sprintf("%+v", e) or access StackTrace() directly.

func (*PanicError) Format

func (e *PanicError) Format(s fmt.State, verb rune)

Format formats the PanicError according to the fmt.Formatter interface. The formatting behavior is consistent with Frame.Format() and StackTrace.Format():

%v    error message only (same as Error())
%+v   error message with detailed stack trace (each frame shows function name, file path, and line)
%s    error message only (same as Error())
%+s   error message with stack trace (each frame shows file name only)

Note: Unlike Frame.Format() which supports 's', 'd', 'n', 'v', PanicError only supports 'v' and 's'. The 'd' (line number) and 'n' (function name) verbs are not applicable to PanicError. For unsupported verbs, the behavior defaults to 's' (error message only).

Similar to Frame.Format(), %v recursively calls Format(s, 's') for the error message part, then conditionally appends stack trace based on flags.

Example:

err := result.Err()
fmt.Printf("%v", err)    // "test error"
fmt.Printf("%+v", err)   // "test error\n\nfunction_name\n\tfile_path:line\n..."
fmt.Printf("%s", err)    // "test error"
fmt.Printf("%+s", err)   // "test error\n\n[file:line file:line ...]"

func (*PanicError) StackTrace

func (e *PanicError) StackTrace() StackTrace

StackTrace returns the stack trace associated with this panic error.

func (*PanicError) Unwrap

func (e *PanicError) Unwrap() error

Unwrap returns the wrapped error.

type StackTrace

type StackTrace []Frame

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

func GetStackTrace

func GetStackTrace(skip int) StackTrace

GetStackTrace gets the stack trace.

func PanicStackTrace

func PanicStackTrace() StackTrace

PanicStackTrace gets the panic stack trace.

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
%d	lists line numbers for each Frame in the stack
%n	lists function names 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.

type StackTraceCarrier

type StackTraceCarrier interface {
	StackTrace() StackTrace
}

StackTraceCarrier is an interface that can carry a stack trace.

Jump to

Keyboard shortcuts

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