Skip to content

Commit

Permalink
Improve main error output
Browse files Browse the repository at this point in the history
Add support for `Unwrap` functions on errors.
  • Loading branch information
HeavyWombat committed Jan 18, 2022
1 parent 140c6d5 commit 9d84ecc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"os"
"strings"

"github.com/gonvenience/bunt"
"github.com/gonvenience/neat"
Expand Down Expand Up @@ -52,6 +53,11 @@ func readableError(err error) string {
var headline = "Error occurred"
var buf bytes.Buffer

type unwrapper interface {
Error() string
Unwrap() error
}

switch terr := err.(type) {
case wrap.ContextError:
headline = fmt.Sprintf("Error: %s", terr.Context())
Expand All @@ -63,6 +69,10 @@ func readableError(err error) string {
buf.WriteString(readableError(e))
}

case unwrapper:
headline = strings.Split(terr.Error(), ":")[0]
buf.WriteString(terr.Unwrap().Error())

default:
buf.WriteString(terr.Error())
}
Expand Down

0 comments on commit 9d84ecc

Please sign in to comment.