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