Documentation ¶
Overview ¶
Package errs implements a detailed error object that provides stack traces with source locations, along with nested causes, if any.
Index ¶
- func Recovery(handler RecoveryHandler)
- func Wrap(cause error) error
- type Causer
- type Error
- func (d *Error) Count() int
- func (d *Error) Detail(trimRuntime bool) string
- func (d Error) Error() string
- func (d *Error) ErrorOrNil() error
- func (d *Error) Format(state fmt.State, verb rune)
- func (d *Error) Message() string
- func (d *Error) RawStackTrace() []uintptr
- func (d *Error) StackTrace(trimRuntime bool) string
- func (d *Error) Unwrap() error
- func (d *Error) WrappedErrors() []error
- type ErrorWrapper
- type RecoveryHandler
- type StackError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Recovery ¶ added in v1.14.0
func Recovery(handler RecoveryHandler)
Recovery provides an easy way to run code that may panic. 'handler' will be called with the panic turned into an error. Pass in nil to silently ignore any panic.
Typical usage:
func runSomeCode(handler errs.RecoveryHandler) { defer errs.Recovery(handler) // ... run the code here ... }
Types ¶
type Causer ¶
type Causer interface {
Cause() error
}
Causer defines the interface for determining the error the caused an error.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error holds the detailed error message.
Example ¶
package main import ( "fmt" "github.com/richardwilkes/toolbox/errs" ) func main() { var bad *int func() int { defer func() { if r := recover(); r != nil { if err, ok := r.(error); ok { fmt.Println(errs.NewWithCause("Caught panic", err)) } } }() return *bad // trigger a panic due to a nil pointer dereference }() }
Output:
func NewWithCause ¶
NewWithCause creates a new detailed error with the 'message' and underlying 'cause'.
func NewWithCausef ¶ added in v1.21.1
NewWithCausef creates a new detailed error with an underlying 'cause' and using fmt.Sprintf() to format the message.
func WrapTyped ¶ added in v1.1.4
WrapTyped wraps an error and turns it into a detailed error. If error is already a detailed error or nil, it will be returned as-is. This method returns the error as an *Error. Use Wrap() to receive a generic error.
func (*Error) Detail ¶
Detail returns the fully detailed error message, which includes the primary message, the call stack, and potentially one or more chained causes.
func (*Error) ErrorOrNil ¶
ErrorOrNil returns an error interface if this Error represents one or more errors, or nil if it is empty.
func (*Error) Format ¶
Format implements the fmt.Formatter interface.
Supported formats:
- "%s" Just the message
- "%q" Just the message, but quoted
- "%v" The message plus a stack trace, trimmed of golang runtime calls
- "%+v" The message plus a stack trace
func (*Error) RawStackTrace ¶ added in v1.21.0
RawStackTrace returns the raw call stack pointers.
func (*Error) StackTrace ¶
StackTrace returns just the stack trace portion of the message.
func (*Error) Unwrap ¶ added in v1.21.0
Unwrap implements errors.Unwrap and returns the underlying cause, if any.
func (*Error) WrappedErrors ¶
WrappedErrors returns the contained errors.
type ErrorWrapper ¶
ErrorWrapper contains methods for interacting with the wrapped errors.
type RecoveryHandler ¶ added in v1.14.0
type RecoveryHandler func(error)
RecoveryHandler defines the callback used when panics occur with Recovery.