github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/panichandler/handler.go (about)

     1  package panichandler
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"text/template"
     9  
    10  	"code.cloudfoundry.org/cli/version"
    11  )
    12  
    13  const maxStackSizeLimit = 1024 * 1024
    14  
    15  // HandlePanic will recover from any panics and display a friendly error
    16  // message with additional information used for debugging the panic.
    17  func HandlePanic() {
    18  	stackTraceBytes := make([]byte, maxStackSizeLimit)
    19  	runtime.Stack(stackTraceBytes, true)
    20  	stackTrace := "\t" + strings.Replace(string(stackTraceBytes), "\n", "\n\t", -1)
    21  	if err := recover(); err != nil {
    22  		formattedString := `
    23  		Something unexpected happened. This is a bug in {{.Binary}}.
    24  
    25  		Please re-run the command that caused this exception with the environment
    26  		variable CF_TRACE set to true.
    27  
    28  		Also, please update to the latest cli and try the command again:
    29  		https://code.cloudfoundry.org/cli/releases
    30  
    31  		Please create an issue at: https://code.cloudfoundry.org/cli/issues
    32  
    33  		Include the below information when creating the issue:
    34  
    35  		Command
    36  		{{.Command}}
    37  
    38  		CLI Version
    39  		{{.Version}}
    40  
    41  		Error
    42  		{{.Error}}
    43  
    44  		Stack Trace
    45  		{{.StackTrace}}
    46  
    47  		Your Platform Details
    48  		e.g. Mac OS X 10.11, Windows 8.1 64-bit, Ubuntu 14.04.3 64-bit
    49  
    50  		Shell
    51  		e.g. Terminal, iTerm, Powershell, Cygwin, gnome-terminal, terminator
    52  		`
    53  		formattedTemplate := template.Must(template.New("Panic Template").Parse(formattedString))
    54  		templateErr := formattedTemplate.Execute(os.Stderr, map[string]interface{}{
    55  			"Binary":     os.Args[0],
    56  			"Command":    strings.Join(os.Args, " "),
    57  			"Version":    version.VersionString(),
    58  			"StackTrace": stackTrace,
    59  			"Error":      err,
    60  		})
    61  		if templateErr != nil {
    62  			fmt.Fprintf(os.Stderr,
    63  				"Unable to format panic response: %s\n",
    64  				templateErr.Error(),
    65  			)
    66  
    67  			fmt.Fprintf(os.Stderr,
    68  				"Version:%s\nCommand:%s\nOriginal Stack Trace:%s\nOriginal Error:%s\n",
    69  				version.VersionString(),
    70  				strings.Join(os.Args, " "),
    71  				stackTrace,
    72  				err,
    73  			)
    74  		}
    75  		os.Exit(1)
    76  	}
    77  }