Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Catcher ¶
type Catcher struct {
// contains filtered or unexported fields
}
Catcher is used to catch panics. You can execute a function with Try, which will catch any spawned panic. Try can be called any number of times, from any number of goroutines. Once all calls to Try have completed, you can get the value of the first panic (if any) with Recovered(), or you can just propagate the panic (re-panic) with Repanic().
func (*Catcher) Recovered ¶
func (p *Catcher) Recovered() *RecoveredPanic
Recovered returns the value of the first panic caught by Try, or nil if no calls to Try panicked.
type ErrRecoveredPanic ¶
type ErrRecoveredPanic struct{ RecoveredPanic }
ErrRecoveredPanic wraps a RecoveredPanic in an error implementation.
func (*ErrRecoveredPanic) Error ¶
func (p *ErrRecoveredPanic) Error() string
func (*ErrRecoveredPanic) Unwrap ¶
func (p *ErrRecoveredPanic) Unwrap() error
type RecoveredPanic ¶
type RecoveredPanic struct {
// The original value of the panic.
Value any
// The caller list as returned by runtime.Callers when the panic was
// recovered. Can be used to produce a more detailed stack information with
// runtime.CallersFrames.
Callers []uintptr
// The formatted stacktrace from the goroutine where the panic was recovered.
// Easier to use than Callers.
Stack []byte
}
RecoveredPanic is a panic that was caught with recover().
func NewRecoveredPanic ¶
func NewRecoveredPanic(skip int, value any) RecoveredPanic
NewRecoveredPanic creates a RecoveredPanic from a panic value and a collected stacktrace. The skip parameter allows the caller to skip stack frames when collecting the stacktrace. Calling with a skip of 0 means include the call to NewRecoveredPanic in the stacktrace.
func Try ¶
func Try(f func()) *RecoveredPanic
Try executes f, catching and returning any panic it might spawn.
The recovered panic can be propagated with panic(), or handled as a normal error with (*RecoveredPanic).AsError().
func (*RecoveredPanic) AsError ¶
func (p *RecoveredPanic) AsError() error
AsError casts the panic into an error implementation. The implementation is unwrappable with the cause of the panic, if the panic was provided one.
func (*RecoveredPanic) String ¶
func (p *RecoveredPanic) String() string
String renders a human-readable formatting of the panic.