-
Notifications
You must be signed in to change notification settings - Fork 48
/
crash_report.go
130 lines (105 loc) · 2.95 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
package github
import (
"bytes"
"errors"
"fmt"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/utils"
"os"
"reflect"
"runtime"
"strings"
)
const (
ghReportCrashConfig = "gh.reportCrash"
ghProjectOwner = "jingweno"
ghProjectName = "gh"
)
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(ghProjectOwner, ghProjectName, GitHubHost)
gh := NewClient(project.Host)
issue, err := gh.CreateIssue(project, title, body, []string{"Crash Report"})
utils.Check(err)
fmt.Println(issue.HTMLURL)
}
func reportTitleAndBody(reportedError error, stack string) (title, body string, err error) {
message := "Crash report - %v\n\nError (%s): `%v`\n\nStack:\n\n```\n%s\n```\n\nRuntime:\n\n```\n%s\n```\n\n"
message += `
# Creating crash report:
#
# This information will be posted as a new issue under jingweno/gh.
# 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.
`
errType := reflect.TypeOf(reportedError).String()
message = fmt.Sprintf(message, reportedError, errType, reportedError, stack, runtimeInfo())
editor, err := NewEditor("CRASH_REPORT", message)
if err != nil {
return "", "", err
}
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) {
fmt.Printf("%v\n\n", err)
fmt.Println(stack)
}
func saveReportConfiguration(confirm string, always bool) {
if always {
git.SetGlobalConfig(ghReportCrashConfig, "always")
} else if utils.IsOption(confirm, "e", "never") {
git.SetGlobalConfig(ghReportCrashConfig, "never")
}
}
func reportCrashConfig() (opt string) {
opt = os.Getenv("GH_REPORT_CRASH")
if opt == "" {
opt, _ = git.GlobalConfig(ghReportCrashConfig)
}
return
}