Skip to content

Commit

Permalink
fix: better development ergonomics
Browse files Browse the repository at this point in the history
When we're developing, we woudn't want to swallow panics since that can
cause a lot of surprises.

For non-development cases, since we're reporting this, then swallowing
the panic and displaying a basic error message would be enough for the
user to know that something went wrong happened.
  • Loading branch information
cyx committed May 14, 2021
1 parent 7fe639a commit e09c599
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 18 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Execute() {
Long: "Supercharge your development workflow.\n" + getLogin(cli),
Version: buildinfo.GetVersionWithCommit(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
panic("ily")
ansi.DisableColors = cli.noColor
prepareInteractivity(cmd)

Expand Down Expand Up @@ -86,7 +87,7 @@ func Execute() {
"no-input", false, "Disable interactivity.")

rootCmd.PersistentFlags().BoolVar(&cli.noColor,
"no-color", false, "Disable colors.")
"no-color", false, "Disable colors.")
// order of the comamnds here matters
// so add new commands in a place that reflect its relevance or relation with other commands:
rootCmd.AddCommand(loginCmd(cli))
Expand All @@ -111,7 +112,15 @@ func Execute() {
defer func() {
if v := recover(); v != nil {
err := fmt.Errorf("panic: %v", v)
instrumentation.ReportException(err)

// If we're in development mode, we should throw the
// panic for so we have less surprises. For
// non-developers, we'll swallow the panics.
if instrumentation.ReportException(err) {
fmt.Println(panicMessage)
} else {
panic(v)
}
}
}()

Expand Down Expand Up @@ -143,3 +152,10 @@ func contextWithSignal(sigs ...os.Signal) context.Context {

return ctx
}

const panicMessage = `
!! Uh oh. Something went wrong.
!! If this problem keeps happening feel free to report an issue at
!!
!! https://github.com/auth0/auth0-cli/issues/new/choose
`
7 changes: 4 additions & 3 deletions internal/instrumentation/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ var SentryDSN string

// ReportException is designed to be called once as the CLI exits. We're
// purposefully initializing a client all the time given this context.
func ReportException(err error) {
func ReportException(err error) bool {
if SentryDSN == "" {
return
return false
}

if err := sentry.Init(sentry.ClientOptions{Dsn: SentryDSN}); err != nil {
return
return false
}

// Flush buffered events before the program terminates.
sentry.CaptureException(err)

// Allow up to 2s to flush, otherwise quit.
sentry.Flush(2 * time.Second)
return true
}

0 comments on commit e09c599

Please sign in to comment.