@@ -13,7 +13,7 @@ import (
1313// get the value of the first panic (if any) with Recovered(), or you can just
1414// propagate the panic (re-panic) with Repanic().
1515type Catcher struct {
16- recovered atomic.Pointer [RecoveredPanic ]
16+ recovered atomic.Pointer [Recovered ]
1717}
1818
1919// Try executes f, catching any panic it might spawn. It is safe
@@ -25,13 +25,13 @@ func (p *Catcher) Try(f func()) {
2525
2626func (p * Catcher ) tryRecover () {
2727 if val := recover (); val != nil {
28- rp := NewRecoveredPanic (1 , val )
28+ rp := NewRecovered (1 , val )
2929 p .recovered .CompareAndSwap (nil , & rp )
3030 }
3131}
3232
3333// Repanic panics if any calls to Try caught a panic. It will panic with the
34- // value of the first panic caught, wrapped in a RecoveredPanic with caller
34+ // value of the first panic caught, wrapped in a panics.Recovered with caller
3535// information.
3636func (p * Catcher ) Repanic () {
3737 if val := p .Recovered (); val != nil {
@@ -41,27 +41,27 @@ func (p *Catcher) Repanic() {
4141
4242// Recovered returns the value of the first panic caught by Try, or nil if
4343// no calls to Try panicked.
44- func (p * Catcher ) Recovered () * RecoveredPanic {
44+ func (p * Catcher ) Recovered () * Recovered {
4545 return p .recovered .Load ()
4646}
4747
48- // NewRecoveredPanic creates a RecoveredPanic from a panic value and a
49- // collected stacktrace. The skip parameter allows the caller to skip stack
50- // frames when collecting the stacktrace. Calling with a skip of 0 means
51- // include the call to NewRecoveredPanic in the stacktrace.
52- func NewRecoveredPanic (skip int , value any ) RecoveredPanic {
48+ // NewRecovered creates a panics.Recovered from a panic value and a collected
49+ // stacktrace. The skip parameter allows the caller to skip stack frames when
50+ // collecting the stacktrace. Calling with a skip of 0 means include the call to
51+ // NewRecovered in the stacktrace.
52+ func NewRecovered (skip int , value any ) Recovered {
5353 // 64 frames should be plenty
5454 var callers [64 ]uintptr
5555 n := runtime .Callers (skip + 1 , callers [:])
56- return RecoveredPanic {
56+ return Recovered {
5757 Value : value ,
5858 Callers : callers [:n ],
5959 Stack : debug .Stack (),
6060 }
6161}
6262
63- // RecoveredPanic is a panic that was caught with recover().
64- type RecoveredPanic struct {
63+ // Recovered is a panic that was caught with recover().
64+ type Recovered struct {
6565 // The original value of the panic.
6666 Value any
6767 // The caller list as returned by runtime.Callers when the panic was
@@ -74,27 +74,27 @@ type RecoveredPanic struct {
7474}
7575
7676// String renders a human-readable formatting of the panic.
77- func (p * RecoveredPanic ) String () string {
77+ func (p * Recovered ) String () string {
7878 return fmt .Sprintf ("panic: %v\n stacktrace:\n %s\n " , p .Value , p .Stack )
7979}
8080
8181// AsError casts the panic into an error implementation. The implementation
8282// is unwrappable with the cause of the panic, if the panic was provided one.
83- func (p * RecoveredPanic ) AsError () error {
83+ func (p * Recovered ) AsError () error {
8484 if p == nil {
8585 return nil
8686 }
87- return & ErrRecoveredPanic {* p }
87+ return & ErrRecovered {* p }
8888}
8989
90- // ErrRecoveredPanic wraps a RecoveredPanic in an error implementation.
91- type ErrRecoveredPanic struct { RecoveredPanic }
90+ // ErrRecovered wraps a panics.Recovered in an error implementation.
91+ type ErrRecovered struct { Recovered }
9292
93- var _ error = (* ErrRecoveredPanic )(nil )
93+ var _ error = (* ErrRecovered )(nil )
9494
95- func (p * ErrRecoveredPanic ) Error () string { return p .String () }
95+ func (p * ErrRecovered ) Error () string { return p .String () }
9696
97- func (p * ErrRecoveredPanic ) Unwrap () error {
97+ func (p * ErrRecovered ) Unwrap () error {
9898 if err , ok := p .Value .(error ); ok {
9999 return err
100100 }
0 commit comments