Skip to content

Commit

Permalink
Annotate errors with context
Browse files Browse the repository at this point in the history
This will prevent situations when program terminates with bare error
leaving the user without any clue of where the error was occurred.
  • Loading branch information
tuxofil authored and anthonyfok committed Aug 26, 2021
1 parent 4de7267 commit 1338367
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 90 deletions.
4 changes: 2 additions & 2 deletions create_salsa_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func execCreateSalsaProject(args []string) {
}

if err := fs.Parse(args); err != nil {
log.Fatal(err)
log.Fatalf("parse: %s", err)
}

if fs.NArg() != 1 {
Expand All @@ -38,7 +38,7 @@ func execCreateSalsaProject(args []string) {

resp, err := http.Post(u.String(), "", nil)
if err != nil {
log.Fatal(err)
log.Fatalf("http post: %s", err)
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
b, _ := ioutil.ReadAll(resp.Body)
Expand Down
9 changes: 5 additions & 4 deletions description.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"

Expand All @@ -22,17 +23,17 @@ func reformatForControl(raw string) string {
func getLongDescriptionForGopkg(gopkg string) (string, error) {
owner, repo, err := findGitHubRepo(gopkg)
if err != nil {
return "", err
return "", fmt.Errorf("find github repo: %w", err)
}

rr, _, err := gitHub.Repositories.GetReadme(context.TODO(), owner, repo, nil)
if err != nil {
return "", err
return "", fmt.Errorf("get readme: %w", err)
}

content, err := rr.GetContent()
if err != nil {
return "", err
return "", fmt.Errorf("get content: %w", err)
}

// Supported filename suffixes are from
Expand All @@ -55,7 +56,7 @@ func getLongDescriptionForGopkg(gopkg string) (string, error) {
cmd.Stdin = bytes.NewBuffer(output)
out, err := cmd.Output()
if err != nil {
return "", err
return "", fmt.Errorf("fmt: %w", err)
}
return reformatForControl(strings.TrimSpace(string(out))), nil
}
Expand Down
18 changes: 9 additions & 9 deletions estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,34 @@ func removeVendor(gopath string) (found bool, _ error) {
}
found = true
if err := os.RemoveAll(path); err != nil {
return err
return fmt.Errorf("remove all: %w", err)
}
return filepath.SkipDir
})
return found, err
return found, fmt.Errorf("walk: %w", err)
}

func estimate(importpath string) error {
// construct a separate GOPATH in a temporary directory
gopath, err := ioutil.TempDir("", "dh-make-golang")
if err != nil {
return err
return fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(gopath)

if err := get(gopath, importpath); err != nil {
return err
return fmt.Errorf("go get: %w", err)
}

found, err := removeVendor(gopath)
if err != nil {
return err
return fmt.Errorf("remove vendor: %w", err)
}

if found {
// Fetch un-vendored dependencies
if err := get(gopath, importpath); err != nil {
return err
return fmt.Errorf("fetch un-vendored: go get: %w", err)
}
}

Expand All @@ -91,7 +91,7 @@ func estimate(importpath string) error {

out, err := cmd.Output()
if err != nil {
return fmt.Errorf("%v: %v", cmd.Args, err)
return fmt.Errorf("go list std: args: %v; error: %w", cmd.Args, err)
}
stdlib := make(map[string]bool)
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
Expand Down Expand Up @@ -187,7 +187,7 @@ func execEstimate(args []string) {

err := fs.Parse(args)
if err != nil {
log.Fatal(err)
log.Fatalf("parse args: %s", err)
}

if fs.NArg() != 1 {
Expand All @@ -198,6 +198,6 @@ func execEstimate(args []string) {
// TODO: support the -git_revision flag

if err := estimate(fs.Arg(0)); err != nil {
log.Fatal(err)
log.Fatalf("estimate: %s", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Debian/dh-make-golang

go 1.12
go 1.13

require (
github.com/google/go-github/v38 v38.1.0
Expand Down
Loading

0 comments on commit 1338367

Please sign in to comment.