Skip to content

Commit

Permalink
Improve wrapped errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLich committed Jan 2, 2022
1 parent 1cdd748 commit 5115f2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 11 additions & 9 deletions regolith/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ func (profile *Profile) Install(isForced bool, profilePath string) error {
for filter := range profile.Filters {
filter := &profile.Filters[filter] // Using pointer is faster than creating copies in the loop and gives more options

downloadPath, err := filter.Download(isForced, profilePath)
downloadPath := filter.GetDownloadUrl()
err := filter.Download(isForced, profilePath)
// TODO - we could use type switch to handle different kinds of errors
// here. Download can fail on downloading or on cleaning the download
// path. It can also fail when isForced is false and the path already
// exists.
if err != nil {
Logger.Warnf("Could not download filter", err)
Logger.Fatal(wrapError("Could not download filter: ", err))
} else if downloadPath == "" { // filter.RunWith != "" && filter.Script != ""
continue
}
Expand Down Expand Up @@ -269,7 +270,7 @@ func (filter *Filter) DownloadDependencies(isForced bool, profileDirectory strin
}

// Downloads the filter into its own directory and returns the download path.
func (filter *Filter) Download(isForced bool, profileDirectory string) (string, error) {
func (filter *Filter) Download(isForced bool, profileDirectory string) error {
url := filter.GetDownloadUrl()

// Download the filter into the cache folder
Expand All @@ -281,24 +282,25 @@ func (filter *Filter) Download(isForced bool, profileDirectory string) (string,
if !isForced {
Logger.Warnf("Dependency %s already installed, skipping. Run "+
"with '-f' to force.", url)
return "", nil
return nil
} else {
Logger.Warnf("Dependency %s already installed and force mode is enabled.", url)
err := os.RemoveAll(downloadPath)
if err != nil {
return "", wrapError("Could not remove installed filter.", err)
return wrapError("Could not remove installed filter.", err)
}
}
} else {

}

Logger.Infof("Installing filter %s...", url)

// Download the filter using Git Getter
// TODO:
// Can we somehow detect whether this is a failure from git being not installed, or a failure from
// the repo/folder not existing?
err := getter.Get(downloadPath, url)
if err != nil {
return "", wrapError("Could not download filter. Is git installed?", err)
return wrapError("Could not download filter. \n Is git installed? \n Does that filter exist?", err)
}

// Remove 'test' folder, which we never want to use
Expand All @@ -307,7 +309,7 @@ func (filter *Filter) Download(isForced bool, profileDirectory string) (string,
os.RemoveAll(testFolder)
}

return downloadPath, nil
return nil
}

type ExportTarget struct {
Expand Down
3 changes: 2 additions & 1 deletion regolith/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"

"github.com/fatih/color"
"github.com/google/go-github/v39/github"
)

Expand All @@ -26,7 +27,7 @@ func StringArrayContains(arr []string, str string) bool {

func wrapError(text string, err error) error {
if err != nil {
return errors.New(fmt.Sprintf("%s\nCaused by: %s", text, err.Error()))
return errors.New(fmt.Sprintf("%s\n[%s]: %s", text, color.RedString("+"), err.Error()))
}
return errors.New(text)
}
Expand Down

0 comments on commit 5115f2f

Please sign in to comment.