From 420991cf4fd9854190623960fe46b19fc75f6d48 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Fri, 15 Apr 2022 18:24:05 +0100 Subject: [PATCH] Implement better errors This commit adds the foundational code for better error handling. It largely draws from the code in gh-cli that allows for better error customization. It addresses issues #1 and #2. --- cmd/root.go | 21 +++++++++++++++++---- internal/pkg/changelog/changelog.go | 17 +++++++++++------ internal/pkg/githubclient/githubclient.go | 4 ++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 350e902..d6c62cc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "os" "time" @@ -13,13 +14,16 @@ import ( ) var version = "dev" +var ErrSilent = errors.New("ErrSilent") // RootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "changelog [args]", - Short: "Create a changelog that adheres to the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format", - Long: "Create a changelog that adheres to the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format", - Version: version, + Use: "changelog", + Short: "Create a changelog that adheres to the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format", + Long: "Create a changelog that adheres to the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format", + Version: version, + SilenceUsage: true, + SilenceErrors: true, RunE: func(command *cobra.Command, args []string) error { s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) @@ -43,10 +47,19 @@ func init() { fmt.Println(err) os.Exit(1) } + + rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + cmd.Println(err) + cmd.Println(cmd.UsageString()) + return ErrSilent + }) } func Execute() int { if err := rootCmd.Execute(); err != nil { + if err != ErrSilent { + fmt.Fprintln(os.Stderr, err) + } return 1 } return 0 diff --git a/internal/pkg/changelog/changelog.go b/internal/pkg/changelog/changelog.go index 2278395..890c53e 100644 --- a/internal/pkg/changelog/changelog.go +++ b/internal/pkg/changelog/changelog.go @@ -15,7 +15,7 @@ import ( func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { client, err := githubclient.NewGitHubClient() if err != nil { - return nil, err + return nil, fmt.Errorf("❌ %s", err) } changeLog := NewChangeLogProperties(client.RepoContext.Owner, client.RepoContext.Repo) @@ -25,7 +25,7 @@ func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { tags, err := client.GetTags() if err != nil { - return nil, err + return nil, fmt.Errorf("❌ could not get tags: %v", err) } // Sort by date, this is mad slow but you can't get at the date @@ -46,7 +46,7 @@ func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { spinner.Suffix = fmt.Sprintf(" Processing tags: 🏷️ %s", tag.GetName()) currentCommit, err := client.GetCommit(tag.GetCommit().GetSHA()) if err != nil { - return nil, err + return nil, fmt.Errorf("❌ could not get commit for tag '%s': %v", tag.GetName(), err) } var nextCommit *github.Commit @@ -57,7 +57,7 @@ func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { } if err != nil { - return nil, err + return nil, fmt.Errorf("❌ could not get next commit: %v", err) } pullRequests, err := client.GetPullRequestsBetweenDates( @@ -65,7 +65,12 @@ func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { currentCommit.GetCommitter().GetDate(), ) if err != nil { - return nil, err + return nil, fmt.Errorf( + "❌ could not get pull requests for range '%s - %s': %v", + nextCommit.GetCommitter().GetDate(), + currentCommit.GetCommitter().GetDate(), + err, + ) } typeMap, err := processPullRequests( @@ -75,7 +80,7 @@ func MakeFullChangelog(spinner *spinner.Spinner) (*ChangeLogProperties, error) { viper.GetStringSlice("excludedLabels"), ) if err != nil { - return nil, err + return nil, fmt.Errorf("❌ could not process pull requests: %v", err) } changeLog.Tags = append(changeLog.Tags, *typeMap) diff --git a/internal/pkg/githubclient/githubclient.go b/internal/pkg/githubclient/githubclient.go index 48e8a28..409ec22 100644 --- a/internal/pkg/githubclient/githubclient.go +++ b/internal/pkg/githubclient/githubclient.go @@ -34,6 +34,10 @@ func NewGitHubClient() (*GitHubClient, error) { currentRepository, err := gh.CurrentRepository() if err != nil { + if strings.Contains(err.Error(), "not a git repository (or any of the parent directories)") { + return nil, fmt.Errorf("the current directory is not a git repository") + } + return nil, err }