Skip to content

Commit

Permalink
feat: nicer errors
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Dec 4, 2024
1 parent de921ee commit f38e44e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
32 changes: 16 additions & 16 deletions go/summarize/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/alecthomas/chroma/quick"
"github.com/fatih/color"
"golang.org/x/term"
)

Expand All @@ -43,9 +44,7 @@ func Run(files []string, hotMetric string, showGraph bool) {

for _, file := range files {
typ, err := getFileType(file)
if err != nil {
panic(err.Error())
}
exitIfError(err)
var w summarizer
var t traceSummary
switch typ {
Expand All @@ -60,10 +59,7 @@ func Run(files []string, hotMetric string, showGraph bool) {
default:
err = errors.New("unknown file type")
}

if err != nil {
panic(err.Error())
}
exitIfError(err)

if w != nil {
workers = append(workers, w)
Expand All @@ -76,22 +72,17 @@ func Run(files []string, hotMetric string, showGraph bool) {
traceCount := len(traces)
if traceCount <= 0 {
s, err := printSummary(hotMetric, workers)
if err != nil {
panic(err.Error())
}
exitIfError(err)
if showGraph {
err := renderQueryGraph(s)
if err != nil {
panic(err.Error())
}
exitIfError(err)
}
return
}

err := checkTraceConditions(traces, workers, hotMetric)
if err != nil {
panic(err.Error())
}
exitIfError(err)

switch traceCount {
case 1:
printTraceSummary(os.Stdout, terminalWidth(), highlightQuery, traces[0])
Expand All @@ -100,6 +91,15 @@ func Run(files []string, hotMetric string, showGraph bool) {
}
}

func exitIfError(err error) {
if err == nil {
return
}
_, _ = color.New(color.FgRed).Fprintln(os.Stderr, err.Error())

os.Exit(1)
}

func printSummary(hotMetric string, workers []summaryWorker) (*Summary, error) {
s, err := NewSummary(hotMetric)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions go/summarize/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func getFileType(filename string) (fileType, error) {

token, err := decoder.Token()
if err != nil {
return unknownFile, fmt.Errorf("error reading token: %v", err)
return unknownFile, fmt.Errorf("error reading json token: %v", err)
}

if delim, ok := token.(json.Delim); !ok || delim != '{' {
Expand All @@ -66,7 +66,7 @@ func getFileType(filename string) (fileType, error) {

keyToken, err := decoder.Token()
if err != nil {
return unknownFile, fmt.Errorf("error reading key token: %v", err)
return unknownFile, fmt.Errorf("error reading json key token: %v", err)
}

key, ok := keyToken.(string)
Expand All @@ -76,7 +76,7 @@ func getFileType(filename string) (fileType, error) {

valueToken, err := decoder.Token()
if err != nil {
return unknownFile, fmt.Errorf("error reading value token: %v", err)
return unknownFile, fmt.Errorf("error reading json value token: %v", err)
}

if key == "fileType" {
Expand Down

0 comments on commit f38e44e

Please sign in to comment.