github.com/haraldrudell/parl@v0.4.176/perrors/panicdetector/panic-detector.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package panicdetector
     7  
     8  const (
     9  	// the fully-qualified function name searched for in a [runtime.Stack] value
    10  	runtimeGopanic = "panic"
    11  )
    12  
    13  // panicDetector holds the function names searched for in
    14  // [runtime.Stack]
    15  type panicDetector struct {
    16  	runtimeDeferInvokerLocation  string
    17  	runtimePanicFunctionLocation string
    18  }
    19  
    20  // panicDetectorOne is a static value facilitating panic detection for the Go runtime
    21  //   - created during package initialization therefore thread-safe
    22  //   - must support current and previous Go versions,
    23  //   - as of 240211: go1.21.0–go1.22.0
    24  //   - used by [panicdetector.Indices]
    25  //   - in a stack trace obtained from [runtime.Stack]:
    26  //   - — function: panic
    27  //   - — file: /opt/homebrew/Cellar/go/1.21.7/libexec/src/runtime/panic.go
    28  //   - — additional Go runtime stack frames are not returned
    29  //     - — the oldest stack frame is the first user function, ie.
    30  //     main function or function provided in go statement
    31  //   - in a frame obtained from [runtime.Callers]:
    32  //   - — [runtime.Frame.Function]: runtime.gopanic
    33  //   - — [runtime.Frame.File]: /opt/homebrew/Cellar/go/1.21.7/libexec/src/runtime/panic.go
    34  //   - — several Go runtime stack frames are returned
    35  //     - — the oldest stack frame is the Go exit function
    36  var panicDetectorOne = &panicDetector{
    37  	runtimeDeferInvokerLocation:  runtimeGopanic,
    38  	runtimePanicFunctionLocation: runtimeGopanic,
    39  }
    40  
    41  // PanicDetectorValues is used by [whynotpanic.WhyNotPanic]
    42  // to explain panic detection
    43  func PanicDetectorValues() (deferS, panicS string) {
    44  	deferS = panicDetectorOne.runtimeDeferInvokerLocation
    45  	panicS = panicDetectorOne.runtimePanicFunctionLocation
    46  	return
    47  }