github.com/haraldrudell/parl@v0.4.176/mains/check-for-panic.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package mains
     7  
     8  import (
     9  	"github.com/haraldrudell/parl"
    10  	"github.com/haraldrudell/parl/perrors"
    11  )
    12  
    13  // checkForPanic returns a non-empty string when err contains a panic
    14  //   - the string contains code location for the panic and the recovery
    15  func checkForPanic(err error) (panicString string) {
    16  	if isPanic, stack, recoveryIndex, panicIndex := perrors.IsPanic(err); isPanic {
    17  		var frames = stack.Frames()
    18  		panicString = parl.Sprintf(
    19  			"\nPANIC detected at %s\n"+
    20  				"A Go panic may indicate a software problem\n"+
    21  				"recovery was made at %s\n",
    22  			frames[panicIndex].String(),
    23  			frames[recoveryIndex].String(),
    24  		)
    25  	}
    26  	return
    27  }