Skip to content

Commit

Permalink
Tidy db error message. Ensure all panics are recovered. Closes #118
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek authored and kaidaguerre committed Mar 4, 2021
1 parent 3e3bf39 commit eca156b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 31 deletions.
31 changes: 26 additions & 5 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
12 changes: 3 additions & 9 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}()

Expand Down
29 changes: 25 additions & 4 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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")
Expand All @@ -229,7 +246,6 @@ func runServiceStatusCmd(cmd *cobra.Command, args []string) {
}
}

logging.LogTime("runServiceStatusCmd end")
}

func printStatus(info *db.RunningDBInstanceInfo) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -296,5 +318,4 @@ to force a shutdown

}

logging.LogTime("runServiceStopCmd end")
}
14 changes: 3 additions & 11 deletions db/interactive_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/shiena/ansicolor"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit eca156b

Please sign in to comment.