Skip to content

Commit

Permalink
fix: better development ergonomics (#294)
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 authored May 14, 2021
1 parent 7fe639a commit 7d7b32e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 17 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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 +111,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 +151,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 7d7b32e

Please sign in to comment.