From eca156b6c637fc91cef430617b6393f5b71c64c6 Mon Sep 17 00:00:00 2001 From: Binaek Sarkar Date: Wed, 3 Mar 2021 20:21:12 +0530 Subject: [PATCH] Tidy db error message. Ensure all panics are recovered. Closes #118 --- cmd/plugin.go | 31 ++++++++++++++++++++++++++----- cmd/query.go | 12 +++--------- cmd/service.go | 29 +++++++++++++++++++++++++---- db/interactive_client.go | 14 +++----------- utils/errors.go | 20 ++++++++++++++++++-- 5 files changed, 75 insertions(+), 31 deletions(-) diff --git a/cmd/plugin.go b/cmd/plugin.go index c5b9bd7fd0..ac3f64b89a 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -6,6 +6,7 @@ import ( "log" "strings" + "github.com/turbot/go-kit/helpers" "github.com/turbot/steampipe/cmdconfig" "github.com/turbot/steampipe/constants" "github.com/turbot/steampipe/db" @@ -58,7 +59,6 @@ Examples: // PluginInstallCmd :: Install a plugin func PluginInstallCmd() *cobra.Command { - var cmd = &cobra.Command{ Use: "install [flags] [registry/org/]name[@version]", Args: cobra.ArbitraryArgs, @@ -173,7 +173,12 @@ Example: func runPluginInstallCmd(cmd *cobra.Command, args []string) { logging.LogTime("runPluginInstallCmd install") - defer logging.LogTime("runPluginInstallCmd end") + defer func() { + logging.LogTime("runPluginInstallCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() // args to 'plugin install' -- one or more plugins to install // These can be simple names ('aws') for "standard" plugins, or @@ -271,7 +276,12 @@ func (u *updateSkip) String() string { func runPluginUpdateCmd(cmd *cobra.Command, args []string) { logging.LogTime("runPluginUpdateCmd install") - defer logging.LogTime("runPluginUpdateCmd end") + defer func() { + logging.LogTime("runPluginUpdateCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() // args to 'plugin update' -- one or more plugins to install // These can be simple names ('aws') for "standard" plugins, or @@ -451,7 +461,12 @@ func refreshConnections() error { func runPluginListCmd(cmd *cobra.Command, args []string) { logging.LogTime("runPluginListCmd list") - defer logging.LogTime("runPluginListCmd end") + defer func() { + logging.LogTime("runPluginListCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() connectionMap, err := getPluginConnectionMap() if err != nil { @@ -475,7 +490,13 @@ func runPluginListCmd(cmd *cobra.Command, args []string) { func runPluginUninstallCmd(cmd *cobra.Command, args []string) { logging.LogTime("runPluginUninstallCmd uninstall") - defer logging.LogTime("runPluginUninstallCmd end") + + defer func() { + logging.LogTime("runPluginUninstallCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() if len(args) == 0 { fmt.Println() diff --git a/cmd/query.go b/cmd/query.go index ffcce21630..450afe3508 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -3,8 +3,8 @@ package cmd import ( "fmt" "log" - "os" + "github.com/turbot/go-kit/helpers" "github.com/turbot/steampipe-plugin-sdk/logging" "github.com/turbot/steampipe/cmdconfig" "github.com/turbot/steampipe/display" @@ -57,17 +57,11 @@ Examples: func runQueryCmd(cmd *cobra.Command, args []string) { logging.LogTime("runQueryCmd start") - defer logging.LogTime("execute end") - log.Println("[TRACE] runQueryCmd") defer func() { + logging.LogTime("runQueryCmd end") if r := recover(); r != nil { - err, ok := r.(error) - if !ok { - err = fmt.Errorf("%v", r) - } - utils.ShowError(err) - os.Exit(1) + utils.ShowError(helpers.ToError(r)) } }() diff --git a/cmd/service.go b/cmd/service.go index daed4322fd..25661d96f8 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/turbot/go-kit/helpers" "github.com/turbot/steampipe-plugin-sdk/logging" "github.com/turbot/steampipe/cmdconfig" @@ -117,6 +118,12 @@ func ServiceRestartCmd() *cobra.Command { func runServiceStartCmd(cmd *cobra.Command, args []string) { logging.LogTime("runServiceStartCmd start") + defer func() { + logging.LogTime("runServiceStartCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() // // TODO(nw) - color me, replace hard-coding with variables / config if cmdconfig.Viper().GetInt("db-port") < 1 || cmdconfig.Viper().GetInt("db-port") > 65535 { @@ -157,11 +164,16 @@ func runServiceStartCmd(cmd *cobra.Command, args []string) { printStatus(info) - logging.LogTime("runServiceStartCmd end") } func runServiceRestartCmd(cmd *cobra.Command, args []string) { logging.LogTime("runServiceRestartCmd start") + defer func() { + logging.LogTime("runServiceRestartCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() currentServiceStatus, err := db.GetStatus() @@ -211,11 +223,16 @@ to force a restart. printStatus(info) } - logging.LogTime("runServiceRestartCmd end") } func runServiceStatusCmd(cmd *cobra.Command, args []string) { logging.LogTime("runServiceStatusCmd status") + defer func() { + logging.LogTime("runServiceStatusCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() if !db.IsInstalled() { fmt.Println("Steampipe database service is NOT installed") @@ -229,7 +246,6 @@ func runServiceStatusCmd(cmd *cobra.Command, args []string) { } } - logging.LogTime("runServiceStatusCmd end") } func printStatus(info *db.RunningDBInstanceInfo) { @@ -265,6 +281,12 @@ Steampipe service is running in the background. func runServiceStopCmd(cmd *cobra.Command, args []string) { logging.LogTime("runServiceStopCmd stop") + defer func() { + logging.LogTime("runServiceStopCmd end") + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) + } + }() force := cmdconfig.Viper().GetBool(constants.ArgForce) status, err := db.StopDB(force) @@ -296,5 +318,4 @@ to force a shutdown } - logging.LogTime("runServiceStopCmd end") } diff --git a/db/interactive_client.go b/db/interactive_client.go index b6edde6db2..a1b810435c 100644 --- a/db/interactive_client.go +++ b/db/interactive_client.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/turbot/go-kit/helpers" "github.com/turbot/steampipe/autocomplete" "github.com/turbot/steampipe/cmdconfig" "github.com/turbot/steampipe/definitions/results" @@ -43,18 +44,9 @@ func (c *InteractiveClient) close() { // InteractiveQuery :: start an interactive prompt and return func (c *InteractiveClient) InteractiveQuery(resultsStreamer *results.ResultStreamer, onCompleteCallback func()) { defer func() { - onCompleteCallback() - - r := recover() - switch r.(type) { - case nil: - // nothing to do - case utils.ExitCode: - // nothing special yet! - default: - // print out whatever we got - fmt.Println(r) + if r := recover(); r != nil { + utils.ShowError(helpers.ToError(r)) } // close the result stream diff --git a/utils/errors.go b/utils/errors.go index 280a7f6ce7..28ce78830d 100644 --- a/utils/errors.go +++ b/utils/errors.go @@ -3,6 +3,7 @@ package utils import ( "fmt" "os" + "strings" "github.com/fatih/color" "github.com/shiena/ansicolor" @@ -30,11 +31,26 @@ func FailOnErrorWithMessage(err error, message string) { } func ShowError(err error) { - fmt.Fprintf(color.Output, "%s: %v\n", colorErr, err) + fmt.Fprintf(color.Output, "%s: %v\n", colorErr, trimDriversFromErrMsg(err.Error())) } func ShowErrorWithMessage(err error, message string) { - fmt.Fprintf(color.Output, "%s: %s - %v\n", colorErr, message, err) + fmt.Fprintf(color.Output, "%s: %s - %v\n", colorErr, message, trimDriversFromErrMsg(err.Error())) +} + +// remove the pq: and rpc error prefixes along +// with all the unnecessary information that comes from the +// drivers +func trimDriversFromErrMsg(msg string) string { + errString := strings.TrimSpace(msg) + if strings.HasPrefix(errString, "pq:") { + errString = strings.TrimSpace(strings.TrimPrefix(errString, "pq:")) + if strings.HasPrefix(errString, "rpc error") { + // trim out "rpc error: code = Unknown desc =" + errString = strings.TrimSpace(errString[33:]) + } + } + return errString } func ShowWarning(warning string) {