diff --git a/cmd/scw/main.go b/cmd/scw/main.go index 4bc3de5e47..8ff594ec46 100644 --- a/cmd/scw/main.go +++ b/cmd/scw/main.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "os" "runtime" + "runtime/debug" "github.com/hashicorp/go-version" "github.com/mattn/go-colorable" @@ -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 @@ -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, diff --git a/internal/sentry/sentry.go b/internal/sentry/sentry.go index 6d9f41fe41..688127be0c 100644 --- a/internal/sentry/sentry.go +++ b/internal/sentry/sentry.go @@ -12,14 +12,14 @@ const ( dsn = "https://a3d5d1ef6ae94810952ab245ce61af17@sentry.scaleway.com/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)