Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PanicCatcher ¶
type PanicCatcher struct {
// contains filtered or unexported fields
}
PanicCatcher 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 Value(), or you can just propagate the panic (re-panic) with Propagate()
Example ¶
var pc PanicCatcher
i := 0
pc.Try(func() { i += 1 })
pc.Try(func() { panic("abort!") })
pc.Try(func() { i += 1 })
rc := pc.Recovered()
fmt.Println(i)
fmt.Println(rc.Value.(string))
Output: 2 abort!
Example (Callers) ¶
var pc PanicCatcher
pc.Try(func() { panic("mayday!") })
recovered := pc.Recovered()
// For debugging, the pre-formatted recovered.Stack is easier to use than
// rc.Callers. This is not used in the example because its output is
// machine-specific.
frames := runtime.CallersFrames(recovered.Callers)
for {
frame, more := frames.Next()
fmt.Println(frame.Function)
if !more {
break
}
}
Output: github.com/sourcegraph/conc.(*PanicCatcher).tryRecover runtime.gopanic github.com/sourcegraph/conc.ExamplePanicCatcher_callers.func1 github.com/sourcegraph/conc.(*PanicCatcher).Try github.com/sourcegraph/conc.ExamplePanicCatcher_callers testing.runExample testing.runExamples testing.(*M).Run main.main runtime.main runtime.goexit
func (*PanicCatcher) Recovered ¶
func (p *PanicCatcher) Recovered() *RecoveredPanic
Recovered returns the value of the first panic caught by Try, or nil if no calls to Try panicked.
func (*PanicCatcher) Repanic ¶
func (p *PanicCatcher) Repanic()
Repanic panics if any calls to Try caught a panic. It will panic with the value of the first panic caught, wrapped in a RecoveredPanic with caller information.
func (*PanicCatcher) Try ¶
func (p *PanicCatcher) Try(f func())
Try executes f, catching any panic it might spawn. It is safe to call from multiple goroutines simultaneously.
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 (*RecoveredPanic) Error ¶
func (c *RecoveredPanic) Error() string
func (*RecoveredPanic) Unwrap ¶
func (c *RecoveredPanic) Unwrap() error
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is the primary building block for scoped concurrency. Goroutines can be spawned in the WaitGroup with the Go method, and calling Wait() will ensure that each of those goroutines exits before continuing. Any panics in a child goroutine will be caught and propagated to the caller of Wait().