Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pmenglund committed Nov 10, 2023
1 parent 144dded commit e445626
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ func interactiveQuery(ctx context.Context, in io.ReadCloser, out io.Writer, rs *
func executeQuery(ctx context.Context, out io.Writer, rs *rockset.RockClient, sql string) {
result, err := rs.Query(ctx, sql)
if err != nil {
slog.Error("query failed", "err", err)
// TODO should this use tui.ShowError()?
_, _ = fmt.Fprintf(out, "%s\n", tui.ErrorStyle.Render("query failed:", err.Error()))
return
}

Expand Down
31 changes: 8 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"time"

"github.com/getsentry/sentry-go"
rockerr "github.com/rockset/rockset-go-client/errors"

"github.com/rockset/cli/cmd"
"github.com/rockset/cli/tui"
)
Expand Down Expand Up @@ -54,8 +52,8 @@ func main() {
_, _ = fmt.Fprintf(os.Stderr, "%s", string(debug.Stack()))
} else {
sentry.CurrentHub().Recover(r)
errorf(fmt.Sprintf("program crash: %v", r))
// TODO log message about the panic being sent to sentry
_, _ = fmt.Fprintf(os.Stderr, "%s %v\n", tui.ErrorStyle.Render("program crash:"), r)
}
os.Exit(1)
}
Expand All @@ -69,22 +67,14 @@ func main() {

root := cmd.NewRootCmd(Version)
if err := root.ExecuteContext(ctx); err != nil {
// TODO allow users to override the error reporting
// TODO log a message that we sent the error
if !errors.Is(err, context.Canceled) {
// we don't capture errors from the Rockset API
var re rockerr.Error
if errors.As(err, &re) {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", tui.RocksetStyle.Render("Rockset error:", err.Error()))
dbg, _ := root.PersistentFlags().GetBool(flag.Debug)
if id := re.GetTraceId(); id != "" && dbg {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", tui.RocksetStyle.Render("Trace ID:", id))
}
} else {
// this captures usage errors too, as there is no way to distinguish it from other errors
sentry.CaptureException(err)
errorf(err.Error())
}
// TODO allow users to override the error reporting
// TODO log a message that we sent the error
// TODO this captures usage errors too, as there is no way to distinguish them from other errors
sentry.CaptureException(err)

dbg, _ := root.PersistentFlags().GetBool(flag.Debug)
tui.ShowError(os.Stderr, dbg, err)
}

os.Exit(1)
Expand All @@ -96,8 +86,3 @@ func main() {
_, _ = fmt.Fprintf(os.Stderr, "\n%s\n", v)
}
}

func errorf(msg string) {
// bold too?
_, _ = fmt.Fprintf(os.Stderr, "%s\n", tui.ErrorStyle.Render("ERROR:", msg))
}
27 changes: 27 additions & 0 deletions tui/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tui

import (
"errors"
"fmt"
"io"

rockerr "github.com/rockset/rockset-go-client/errors"
)

func ShowError(out io.Writer, debug bool, err error) {
var re rockerr.Error
if errors.As(err, &re) {
if re.GetType() == "" {
_, _ = fmt.Fprintf(out, "%s\n", ErrorStyle.Render("Error:", err.Error()))
} else {
_, _ = fmt.Fprintf(out, "%s\n", RocksetStyle.Render("Rockset error:", err.Error()))
}

if id := re.GetTraceId(); id != "" && debug {
_, _ = fmt.Fprintf(out, "%s\n", RocksetStyle.Render("Trace ID:", id))
}
} else {
_, _ = fmt.Fprintf(out, "%s\n", ErrorStyle.Render("Error:", err.Error()))
}

}

0 comments on commit e445626

Please sign in to comment.