feat(main): humanize some errors

This commit is contained in:
iyear 2023-12-24 15:53:01 +08:00
parent a77e9d6d81
commit 296d9285de

15
main.go
View File

@ -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)
}