forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crash_report.go
147 lines (120 loc) · 3.16 KB
/
crash_report.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package github
import (
"bytes"
"errors"
"fmt"
"os"
"reflect"
"runtime"
"strings"
"github.com/github/hub/git"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
"github.com/github/hub/version"
)
const (
hubReportCrashConfig = "hub.reportCrash"
hubProjectOwner = "github"
hubProjectName = "hub"
)
func CaptureCrash() {
if rec := recover(); rec != nil {
if err, ok := rec.(error); ok {
reportCrash(err)
} else if err, ok := rec.(string); ok {
reportCrash(errors.New(err))
}
}
}
func reportCrash(err error) {
if err == nil {
return
}
buf := make([]byte, 10000)
runtime.Stack(buf, false)
stack := formatStack(buf)
switch reportCrashConfig() {
case "always":
report(err, stack)
case "never":
printError(err, stack)
default:
printError(err, stack)
fmt.Print("Would you like to open an issue? ([Y]es/[N]o/[A]lways/N[e]ver): ")
var confirm string
fmt.Scan(&confirm)
always := utils.IsOption(confirm, "a", "always")
if always || utils.IsOption(confirm, "y", "yes") {
report(err, stack)
}
saveReportConfiguration(confirm, always)
}
os.Exit(1)
}
func report(reportedError error, stack string) {
title, body, err := reportTitleAndBody(reportedError, stack)
utils.Check(err)
project := NewProject(hubProjectOwner, hubProjectName, GitHubHost)
gh := NewClient(project.Host)
issue, err := gh.CreateIssue(project, title, body, []string{"Crash Report"})
utils.Check(err)
ui.Println(issue.HTMLURL)
}
const crashReportTmpl = "Crash report - %v\n\n" +
"Error (%s): `%v`\n\n" +
"Stack:\n\n```\n%s\n```\n\n" +
"Runtime:\n\n```\n%s\n```\n\n" +
"Version:\n\n```\n%s\n```\n" +
`
# Creating crash report:
#
# This information will be posted as a new issue under github/hub.
# We're NOT including any information about the command that you were executing,
# but knowing a little bit more about it would really help us to solve this problem.
# Feel free to modify the title and the description for this issue.
`
func reportTitleAndBody(reportedError error, stack string) (title, body string, err error) {
errType := reflect.TypeOf(reportedError).String()
message := fmt.Sprintf(
crashReportTmpl,
reportedError,
errType,
reportedError,
stack,
runtimeInfo(),
version.FullVersion(),
)
editor, err := NewEditor("CRASH_REPORT", "crash report", message)
if err != nil {
return "", "", err
}
defer editor.DeleteFile()
return editor.EditTitleAndBody()
}
func runtimeInfo() string {
return fmt.Sprintf("GOOS: %s\nGOARCH: %s", runtime.GOOS, runtime.GOARCH)
}
func formatStack(buf []byte) string {
buf = bytes.Trim(buf, "\x00")
stack := strings.Split(string(buf), "\n")
stack = append(stack[0:1], stack[5:]...)
return strings.Join(stack, "\n")
}
func printError(err error, stack string) {
ui.Printf("%v\n\n", err)
ui.Println(stack)
}
func saveReportConfiguration(confirm string, always bool) {
if always {
git.SetGlobalConfig(hubReportCrashConfig, "always")
} else if utils.IsOption(confirm, "e", "never") {
git.SetGlobalConfig(hubReportCrashConfig, "never")
}
}
func reportCrashConfig() (opt string) {
opt = os.Getenv("HUB_REPORT_CRASH")
if opt == "" {
opt, _ = git.GlobalConfig(hubReportCrashConfig)
}
return
}