diff --git a/panics/panics.go b/panics/panics.go index 5d4f12d..abbed7f 100644 --- a/panics/panics.go +++ b/panics/panics.go @@ -13,7 +13,7 @@ import ( // get the value of the first panic (if any) with Recovered(), or you can just // propagate the panic (re-panic) with Repanic(). type Catcher struct { - recovered atomic.Pointer[RecoveredPanic] + recovered atomic.Pointer[Recovered] } // Try executes f, catching any panic it might spawn. It is safe @@ -25,13 +25,13 @@ func (p *Catcher) Try(f func()) { func (p *Catcher) tryRecover() { if val := recover(); val != nil { - rp := NewRecoveredPanic(1, val) + rp := NewRecovered(1, val) p.recovered.CompareAndSwap(nil, &rp) } } // 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 +// value of the first panic caught, wrapped in a panics.Recovered with caller // information. func (p *Catcher) Repanic() { if val := p.Recovered(); val != nil { @@ -41,27 +41,27 @@ func (p *Catcher) Repanic() { // Recovered returns the value of the first panic caught by Try, or nil if // no calls to Try panicked. -func (p *Catcher) Recovered() *RecoveredPanic { +func (p *Catcher) Recovered() *Recovered { return p.recovered.Load() } -// 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 NewRecoveredPanic(skip int, value any) RecoveredPanic { +// NewRecovered creates a panics.Recovered 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 +// NewRecovered in the stacktrace. +func NewRecovered(skip int, value any) Recovered { // 64 frames should be plenty var callers [64]uintptr n := runtime.Callers(skip+1, callers[:]) - return RecoveredPanic{ + return Recovered{ Value: value, Callers: callers[:n], Stack: debug.Stack(), } } -// RecoveredPanic is a panic that was caught with recover(). -type RecoveredPanic struct { +// Recovered is a panic that was caught with recover(). +type Recovered struct { // The original value of the panic. Value any // The caller list as returned by runtime.Callers when the panic was @@ -74,27 +74,27 @@ type RecoveredPanic struct { } // String renders a human-readable formatting of the panic. -func (p *RecoveredPanic) String() string { +func (p *Recovered) String() string { return fmt.Sprintf("panic: %v\nstacktrace:\n%s\n", p.Value, p.Stack) } // 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 (p *RecoveredPanic) AsError() error { +func (p *Recovered) AsError() error { if p == nil { return nil } - return &ErrRecoveredPanic{*p} + return &ErrRecovered{*p} } -// ErrRecoveredPanic wraps a RecoveredPanic in an error implementation. -type ErrRecoveredPanic struct{ RecoveredPanic } +// ErrRecovered wraps a panics.Recovered in an error implementation. +type ErrRecovered struct{ Recovered } -var _ error = (*ErrRecoveredPanic)(nil) +var _ error = (*ErrRecovered)(nil) -func (p *ErrRecoveredPanic) Error() string { return p.String() } +func (p *ErrRecovered) Error() string { return p.String() } -func (p *ErrRecoveredPanic) Unwrap() error { +func (p *ErrRecovered) Unwrap() error { if err, ok := p.Value.(error); ok { return err } diff --git a/panics/panics_test.go b/panics/panics_test.go index f75d444..15bb32e 100644 --- a/panics/panics_test.go +++ b/panics/panics_test.go @@ -143,7 +143,7 @@ func TestCatcher(t *testing.T) { }) } -func TestRecoveredPanicAsError(t *testing.T) { +func TestRecoveredAsError(t *testing.T) { t.Parallel() t.Run("as error is nil", func(t *testing.T) { t.Parallel() diff --git a/panics/try.go b/panics/try.go index d4e0961..4ded92a 100644 --- a/panics/try.go +++ b/panics/try.go @@ -3,8 +3,8 @@ package panics // 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 Try(f func()) *RecoveredPanic { +// (*panics.Recovered).AsError(). +func Try(f func()) *Recovered { var c Catcher c.Try(f) return c.Recovered() diff --git a/waitgroup.go b/waitgroup.go index 6fc1627..47b1bc1 100644 --- a/waitgroup.go +++ b/waitgroup.go @@ -43,8 +43,8 @@ func (h *WaitGroup) Wait() { } // WaitAndRecover will block until all goroutines spawned with Go exit and -// will return a *panics.RecoveredPanic if one of the child goroutines panics. -func (h *WaitGroup) WaitAndRecover() *panics.RecoveredPanic { +// will return a *panics.Recovered if one of the child goroutines panics. +func (h *WaitGroup) WaitAndRecover() *panics.Recovered { h.wg.Wait() // Return a recovered panic if we caught one from a child goroutine.