Skip to content

Commit

Permalink
Catch all panics and report them (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
crufter committed Sep 15, 2020
1 parent 4f55c57 commit 20a9b47
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 0 additions & 2 deletions client/cli/signup/cli.go
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/cmd.go
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"os"
"os/exec"
"runtime/debug"
"sort"
"strings"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
)
)
3 changes: 3 additions & 0 deletions internal/report/report.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 20a9b47

Please sign in to comment.