From 296d9285de5e64f658ae7e36a6bc78ceb8bea651 Mon Sep 17 00:00:00 2001 From: iyear Date: Sun, 24 Dec 2023 15:53:01 +0800 Subject: [PATCH] feat(main): humanize some errors --- main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/main.go b/main.go index 0298a07..43244cc 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,10 @@ import ( "os" "os/signal" + surveyterm "github.com/AlecAivazis/survey/v2/terminal" "github.com/fatih/color" + "github.com/go-faster/errors" + "go.etcd.io/bbolt" "github.com/iyear/tdl/cmd" ) @@ -14,7 +17,19 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() + humanizeErrors := map[error]string{ + bbolt.ErrTimeout: "Current database is used by another process, please terminate it first", + surveyterm.InterruptErr: "Interrupted", + } + if err := cmd.New().ExecuteContext(ctx); err != nil { + for e, m := range humanizeErrors { + if errors.Is(err, e) { + color.Red("%s", m) + os.Exit(1) + } + } + color.Red("Error: %+v", err) os.Exit(1) }