Skip to content

Commit

Permalink
Merge pull request #96 from sourcegraph/panics-naming-simplification
Browse files Browse the repository at this point in the history
panics: reduce naming stutter in panics.RecoveredPanic
  • Loading branch information
bobheadxi committed Feb 25, 2023
2 parents 771c4b0 + 279d23c commit e2914e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
40 changes: 20 additions & 20 deletions panics/panics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion panics/panics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions panics/try.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions waitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e2914e5

Please sign in to comment.