Skip to content

Commit

Permalink
Factor out a function to turn a KeyValue error into a string
Browse files Browse the repository at this point in the history
I can use this in several places to include stderr in the error dialog,
when available.
  • Loading branch information
gcla committed Jul 10, 2022
1 parent 0578a1c commit cd968da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
14 changes: 1 addition & 13 deletions ui/prochandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package ui
import (
"fmt"
"os"
"sort"
"strings"
"time"

"github.com/gcla/gowid"
Expand Down Expand Up @@ -125,17 +123,7 @@ func (t updatePacketViews) OnError(code pcap.HandlerCode, app gowid.IApp, err er
if !profiles.ConfBool("main.suppress-tshark-errors", true) {
var errstr string
if kverr, ok := err.(gowid.KeyValueError); ok {
errstr = fmt.Sprintf("%v\n\n", kverr.Cause())
kvs := make([]string, 0, len(kverr.KeyVals))
ks := make([]string, 0, len(kverr.KeyVals))
for k := range kverr.KeyVals {
ks = append(ks, k)
}
sort.Sort(sort.StringSlice(ks))
for _, k := range ks {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, kverr.KeyVals[k]))
}
errstr = errstr + strings.Join(kvs, "\n\n")
errstr = termshark.KeyValueErrorString(kverr)
} else {
errstr = fmt.Sprintf("%v", err)
}
Expand Down
10 changes: 2 additions & 8 deletions ui/streamui.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package ui
import (
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -343,18 +342,13 @@ func (t *streamParseHandler) OnError(code pcap.HandlerCode, app gowid.IApp, err
} else if !profiles.ConfBool("main.suppress-tshark-errors", true) {
var errstr string
if kverr, ok := err.(gowid.KeyValueError); ok {
errstr = fmt.Sprintf("%v\n\n", kverr.Cause())
kvs := make([]string, 0, len(kverr.KeyVals))
for k, v := range kverr.KeyVals {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, v))
}
errstr = errstr + strings.Join(kvs, "\n")
errstr = termshark.KeyValueErrorString(kverr)
} else {
errstr = fmt.Sprintf("%v", err)
}

app.Run(gowid.RunFunction(func(app gowid.IApp) {
OpenError(errstr, app)
OpenLongError(errstr, app)
}))
}
}
Expand Down
18 changes: 18 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,24 @@ func ErrLogger(key string, val string) *io.PipeWriter {
return log.NewEntry(l).WithField(key, val).WriterLevel(log.ErrorLevel)
}

// KeyValueErrorString returns a string representation of
// a gowid KeyValueError intended to be suitable for displaying in
// a termshark error dialog.
func KeyValueErrorString(err gowid.KeyValueError) string {
res := fmt.Sprintf("%v\n\n", err.Cause())
kvs := make([]string, 0, len(err.KeyVals))
ks := make([]string, 0, len(err.KeyVals))
for k := range err.KeyVals {
ks = append(ks, k)
}
sort.Sort(sort.StringSlice(ks))
for _, k := range ks {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, err.KeyVals[k]))
}
res = res + strings.Join(kvs, "\n\n")
return res
}

//======================================================================

// Need to publish fields for template use
Expand Down

0 comments on commit cd968da

Please sign in to comment.