Skip to content

Commit e2914e5

Browse files
authored
Merge pull request #96 from sourcegraph/panics-naming-simplification
panics: reduce naming stutter in panics.RecoveredPanic
2 parents 771c4b0 + 279d23c commit e2914e5

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

panics/panics.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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().
1515
type 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

2626
func (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.
3636
func (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\nstacktrace:\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
}

panics/panics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestCatcher(t *testing.T) {
143143
})
144144
}
145145

146-
func TestRecoveredPanicAsError(t *testing.T) {
146+
func TestRecoveredAsError(t *testing.T) {
147147
t.Parallel()
148148
t.Run("as error is nil", func(t *testing.T) {
149149
t.Parallel()

panics/try.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package panics
33
// Try executes f, catching and returning any panic it might spawn.
44
//
55
// The recovered panic can be propagated with panic(), or handled as a normal error with
6-
// (*RecoveredPanic).AsError().
7-
func Try(f func()) *RecoveredPanic {
6+
// (*panics.Recovered).AsError().
7+
func Try(f func()) *Recovered {
88
var c Catcher
99
c.Try(f)
1010
return c.Recovered()

waitgroup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func (h *WaitGroup) Wait() {
4343
}
4444

4545
// WaitAndRecover will block until all goroutines spawned with Go exit and
46-
// will return a *panics.RecoveredPanic if one of the child goroutines panics.
47-
func (h *WaitGroup) WaitAndRecover() *panics.RecoveredPanic {
46+
// will return a *panics.Recovered if one of the child goroutines panics.
47+
func (h *WaitGroup) WaitAndRecover() *panics.Recovered {
4848
h.wg.Wait()
4949

5050
// Return a recovered panic if we caught one from a child goroutine.

0 commit comments

Comments
 (0)