From 20a9b4710c811882dfc697c840079ff556ff3b4b Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Tue, 15 Sep 2020 17:55:29 +0200 Subject: [PATCH] Catch all panics and report them (#1365) --- client/cli/signup/cli.go | 2 -- cmd/cmd.go | 8 ++++++++ go.mod | 2 +- internal/report/report.go | 3 +++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/client/cli/signup/cli.go b/client/cli/signup/cli.go index 68821c7c989..70f9ac61215 100644 --- a/client/cli/signup/cli.go +++ b/client/cli/signup/cli.go @@ -67,7 +67,6 @@ func Run(ctx *cli.Context) error { }, cl.WithRequestTimeout(10*time.Second)) if err != nil { fmt.Printf("Error verifying: %s\n", err) - report.Errorf(ctx, "%v: Error verifying: %s", email, err) os.Exit(1) } @@ -161,7 +160,6 @@ func Run(ctx *cli.Context) error { }, cl.WithRequestTimeout(30*time.Second)) if err != nil { fmt.Printf("Error completing signup: %s\n", err) - report.Errorf(ctx, "Error completing signup: %s", err) os.Exit(1) } diff --git a/cmd/cmd.go b/cmd/cmd.go index a19ec9444f8..fcc96f573ea 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -8,6 +8,7 @@ import ( "math/rand" "os" "os/exec" + "runtime/debug" "sort" "strings" "time" @@ -24,6 +25,7 @@ import ( uconf "github.com/micro/micro/v3/internal/config" "github.com/micro/micro/v3/internal/helper" "github.com/micro/micro/v3/internal/network" + "github.com/micro/micro/v3/internal/report" _ "github.com/micro/micro/v3/internal/usage" "github.com/micro/micro/v3/internal/wrapper" "github.com/micro/micro/v3/plugin" @@ -523,6 +525,12 @@ func (c *command) Init(opts ...Option) error { } func (c *command) Run() error { + defer func() { + if r := recover(); r != nil { + report.Errorf(nil, fmt.Sprintf("panic: %v", string(debug.Stack()))) + panic(r) + } + }() return c.app.Run(os.Args) } diff --git a/go.mod b/go.mod index d5fca7c308f..202d27c110e 100644 --- a/go.mod +++ b/go.mod @@ -34,4 +34,4 @@ require ( google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 google.golang.org/grpc v1.27.0 google.golang.org/protobuf v1.25.0 -) \ No newline at end of file +) diff --git a/internal/report/report.go b/internal/report/report.go index 3b5c4a45fd2..fe79b4ddcda 100644 --- a/internal/report/report.go +++ b/internal/report/report.go @@ -84,6 +84,9 @@ type TrackingData struct { } func getTrackingCategory(ctx *cli.Context) string { + if ctx == nil { + return "cli" + } command := helper.Command(ctx) subcommand := helper.Subcommand(ctx) if len(strings.TrimSpace(subcommand)) == 0 {