github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/safe.go (about)

     1  package store
     2  
     3  import (
     4  	"fmt"
     5  	"runtime/debug"
     6  )
     7  
     8  // Execute the function from a goroutine, recovering any panics and exiting
     9  // the program gracefully.
    10  //
    11  // This helps tcell reset the terminal to a state where we can
    12  // print the error correctly, see
    13  // https://github.com/gdamore/tcell/issues/147
    14  // for more discussion.
    15  //
    16  // Otherwise, a panic() would just blow up the terminal and
    17  // force a reset.
    18  func SafeGo(store RStore, f func()) {
    19  	go func() {
    20  		defer func() {
    21  			r := recover()
    22  			if r != nil {
    23  				err := fmt.Errorf("PANIC: %v\n%s", r, debug.Stack())
    24  				store.Dispatch(PanicAction{Err: err})
    25  			}
    26  		}()
    27  
    28  		f()
    29  	}()
    30  }