Skip to content

feat: add banner about scw feedback bug when a panic occurs #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions cmd/scw/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"os"
"runtime"
"runtime/debug"

"github.com/hashicorp/go-version"
"github.com/mattn/go-colorable"
Expand All @@ -28,6 +30,18 @@ var (
GoArch = runtime.GOARCH
)

func cleanup(buildInfo *core.BuildInfo) {
if err := recover(); err != nil {
fmt.Println(sentry.ErrorBanner)
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))

// This will send an anonymous report on Scaleway's sentry.
if buildInfo.IsRelease() {
sentry.RecoverPanicAndSendReport(buildInfo, err)
}
}
}

func main() {
buildInfo := &core.BuildInfo{
Version: version.Must(version.NewSemver(Version)), // panic when version does not respect semantic versionning
Expand All @@ -38,11 +52,7 @@ func main() {
GoOS: GoOS,
GoArch: GoArch,
}

// Catch every panic after this line. This will send an anonymous report on Scaleway's sentry.
if buildInfo.IsRelease() {
defer sentry.RecoverPanicAndSendReport(buildInfo)
}
defer cleanup(buildInfo)

exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{
Args: os.Args,
Expand Down
12 changes: 6 additions & 6 deletions internal/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const (
dsn = "https://[email protected]/186"
)

const ErrorBanner = `---------------------------------------------------------------------------------------
An error occurred, we are sorry, please consider opening a ticket on github using: 'scw feedback bug'
Give us as many details as possible so we can reproduce the error and fix it.
---------------------------------------------------------------------------------------`

// RecoverPanicAndSendReport will recover error if any, log them, and send them to sentry.
// It must be called with the defer built-in.
func RecoverPanicAndSendReport(buildInfo *core.BuildInfo) {
e := recover()
if e == nil {
return
}

func RecoverPanicAndSendReport(buildInfo *core.BuildInfo, e interface{}) {
sentryClient, err := newSentryClient(buildInfo)
if err != nil {
logger.Debugf("cannot create sentry client: %s", err)
Expand Down