From f0bd184bb824b1dab3b8c163b3b564487bbd264d Mon Sep 17 00:00:00 2001 From: Michael Dwan Date: Thu, 19 Aug 2021 18:24:23 -0600 Subject: [PATCH] add ErrorDescription and ErrorSuggestion interfaces for better friendly error output --- internal/flyerr/flyerr.go | 46 +++++++++++++++++++++++++++++++++++++++ main.go | 8 +++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/internal/flyerr/flyerr.go b/internal/flyerr/flyerr.go index 8c43d5ccc1..93b34b6f5a 100644 --- a/internal/flyerr/flyerr.go +++ b/internal/flyerr/flyerr.go @@ -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 @@ -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 { diff --git a/main.go b/main.go index 5d7b126c99..49f49a0a6f 100644 --- a/main.go +++ b/main.go @@ -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