Skip to content

Commit

Permalink
add ErrorDescription and ErrorSuggestion interfaces for better friend…
Browse files Browse the repository at this point in the history
…ly error output
  • Loading branch information
michaeldwan committed Aug 20, 2021
1 parent 473a411 commit f0bd184
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
46 changes: 46 additions & 0 deletions internal/flyerr/flyerr.go
Expand Up @@ -3,10 +3,42 @@ package flyerr
import (
"context"
"errors"
"fmt"

"github.com/logrusorgru/aurora"
)

// ErrAbort is an error for when the CLI aborts
var ErrAbort = errors.New("abort")

// ErrorDescription is an error with detailed description that will be printed before the CLI exits
type ErrorDescription interface {
error
Description() string
}

func GetErrorDescription(err error) string {
var ferr ErrorDescription
if errors.As(err, &ferr) {
return ferr.Description()
}
return ""
}

// ErrorSuggestion is an error with a suggested next steps that will be printed before the CLI exits
type ErrorSuggestion interface {
error
Suggestion() string
}

func GetErrorSuggestion(err error) string {
var ferr ErrorSuggestion
if errors.As(err, &ferr) {
return ferr.Suggestion()
}
return ""
}

func PrintCLIOutput(err error) {
if err == nil {
return
Expand All @@ -18,6 +50,20 @@ func PrintCLIOutput(err error) {

fmt.Println()
fmt.Println(aurora.Red("Error"), err)

description := GetErrorDescription(err)
suggestion := GetErrorSuggestion(err)

if description != "" {
fmt.Printf("\n%s", description)
}

if suggestion != "" {
if description != "" {
fmt.Println()
}
fmt.Printf("\n%s", suggestion)
}
}

func IsCancelledError(err error) bool {
Expand Down
8 changes: 4 additions & 4 deletions main.go
Expand Up @@ -114,10 +114,10 @@ func checkErr(err error) {
safeExit()
}

func isCancelledError(err error) bool {
if err == cmd.ErrAbort {
return true
}
// func isCancelledError(err error) bool {
// if errors.Is(err, cmd.ErrAbort) {
// return true
// }

// if errors.Is(err, context.Canceled) {
// return true
Expand Down

0 comments on commit f0bd184

Please sign in to comment.