Skip to content

Commit

Permalink
Merge pull request #9 from chelnak/better_error_handling
Browse files Browse the repository at this point in the history
Implement better errors
  • Loading branch information
chelnak authored Apr 15, 2022
2 parents 42d4c93 + 420991c commit 25fbbd8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
21 changes: 17 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"
"time"
Expand All @@ -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)
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions internal/pkg/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -57,15 +57,20 @@ 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(
nextCommit.GetCommitter().GetDate(),
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(
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/githubclient/githubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 25fbbd8

Please sign in to comment.