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 ¶
- type ErrBox
- func (e *ErrBox) As(target any) bool
- func (e *ErrBox) GoString() string
- func (e *ErrBox) Is(target error) bool
- func (e *ErrBox) IsEmpty() bool
- func (e *ErrBox) String() string
- func (e *ErrBox) ToError() error
- func (e *ErrBox) Unwrap() error
- func (e *ErrBox) Value() any
- func (e *ErrBox) ValueOrDefault() any
- type Frame
- type PanicError
- type StackTrace
- type StackTraceCarrier
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 (*ErrBox) As ¶
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 ¶
GoString returns the Go-syntax representation. This implements the fmt.GoStringer interface.
func (*ErrBox) String ¶
String returns the string representation. This implements the fmt.Stringer interface.
func (*ErrBox) ToError ¶
ToError converts ErrBox to error interface. Returns nil if the receiver is nil or val is nil. Returns a pointer to innerErrBox which implements the error interface.
Example:
```go
var eb errutil.ErrBox = errutil.BoxErr(errors.New("test"))
var err error = eb.ToError() // err is *innerErrBox implementing error
```
func (*ErrBox) ValueOrDefault ¶
ValueOrDefault returns the inner value, or nil if ErrBox is nil or val is nil. This is useful when you want to safely extract a typed value from ErrBox.
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 ¶
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 ¶
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.
type StackTrace ¶
type StackTrace []Frame
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
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.