Skip to content

Commit a3b30aa

Browse files
authored
feat: add banner about scw feedback bug when a panic occurs (#1813)
1 parent bad8bcc commit a3b30aa

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

cmd/scw/main.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
"runtime"
7+
"runtime/debug"
68

79
"github.com/hashicorp/go-version"
810
"github.com/mattn/go-colorable"
@@ -28,6 +30,18 @@ var (
2830
GoArch = runtime.GOARCH
2931
)
3032

33+
func cleanup(buildInfo *core.BuildInfo) {
34+
if err := recover(); err != nil {
35+
fmt.Println(sentry.ErrorBanner)
36+
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
37+
38+
// This will send an anonymous report on Scaleway's sentry.
39+
if buildInfo.IsRelease() {
40+
sentry.RecoverPanicAndSendReport(buildInfo, err)
41+
}
42+
}
43+
}
44+
3145
func main() {
3246
buildInfo := &core.BuildInfo{
3347
Version: version.Must(version.NewSemver(Version)), // panic when version does not respect semantic versionning
@@ -38,11 +52,7 @@ func main() {
3852
GoOS: GoOS,
3953
GoArch: GoArch,
4054
}
41-
42-
// Catch every panic after this line. This will send an anonymous report on Scaleway's sentry.
43-
if buildInfo.IsRelease() {
44-
defer sentry.RecoverPanicAndSendReport(buildInfo)
45-
}
55+
defer cleanup(buildInfo)
4656

4757
exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{
4858
Args: os.Args,

internal/sentry/sentry.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ const (
1212
dsn = "https://[email protected]/186"
1313
)
1414

15+
const ErrorBanner = `---------------------------------------------------------------------------------------
16+
An error occurred, we are sorry, please consider opening a ticket on github using: 'scw feedback bug'
17+
Give us as many details as possible so we can reproduce the error and fix it.
18+
---------------------------------------------------------------------------------------`
19+
1520
// RecoverPanicAndSendReport will recover error if any, log them, and send them to sentry.
1621
// It must be called with the defer built-in.
17-
func RecoverPanicAndSendReport(buildInfo *core.BuildInfo) {
18-
e := recover()
19-
if e == nil {
20-
return
21-
}
22-
22+
func RecoverPanicAndSendReport(buildInfo *core.BuildInfo, e interface{}) {
2323
sentryClient, err := newSentryClient(buildInfo)
2424
if err != nil {
2525
logger.Debugf("cannot create sentry client: %s", err)

0 commit comments

Comments
 (0)