Added actual error message to recover error output

This commit is contained in:
bytedream 2022-03-14 23:42:28 +01:00
parent b1945d672d
commit 4d538dfc0c

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"context"
"github.com/ByteDream/crunchyroll-go" "github.com/ByteDream/crunchyroll-go"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http" "net/http"
@ -21,6 +22,10 @@ var (
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "crunchyroll", Use: "crunchyroll",
Short: "Download crunchyroll videos with ease", Short: "Download crunchyroll videos with ease",
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if verboseFlag { if verboseFlag {
out = newLogger(true, true, true) out = newLogger(true, true, true)
@ -46,14 +51,17 @@ func Execute() {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
if out.IsDev() { if out.IsDev() {
out.Err(string(debug.Stack())) out.Err("%v: %s", r, debug.Stack())
} else { } else {
out.Err("Unexpected error: %v", r) out.Err("Unexpected error: %v", r)
} }
os.Exit(1) os.Exit(1)
} }
}() }()
if rootCmd.Execute() != nil { if err := rootCmd.Execute(); err != nil {
if err != context.Canceled {
out.Exit("An error occurred: %v", err)
}
os.Exit(1) os.Exit(1)
} }
} }