Skip to content

Commit 1338367

Browse files
tuxofilanthonyfok
authored andcommitted
Annotate errors with context
This will prevent situations when program terminates with bare error leaving the user without any clue of where the error was occurred.
1 parent 4de7267 commit 1338367

9 files changed

+91
-90
lines changed

create_salsa_project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func execCreateSalsaProject(args []string) {
1919
}
2020

2121
if err := fs.Parse(args); err != nil {
22-
log.Fatal(err)
22+
log.Fatalf("parse: %s", err)
2323
}
2424

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

3939
resp, err := http.Post(u.String(), "", nil)
4040
if err != nil {
41-
log.Fatal(err)
41+
log.Fatalf("http post: %s", err)
4242
}
4343
if got, want := resp.StatusCode, http.StatusOK; got != want {
4444
b, _ := ioutil.ReadAll(resp.Body)

description.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"os/exec"
78
"strings"
89

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

2829
rr, _, err := gitHub.Repositories.GetReadme(context.TODO(), owner, repo, nil)
2930
if err != nil {
30-
return "", err
31+
return "", fmt.Errorf("get readme: %w", err)
3132
}
3233

3334
content, err := rr.GetContent()
3435
if err != nil {
35-
return "", err
36+
return "", fmt.Errorf("get content: %w", err)
3637
}
3738

3839
// Supported filename suffixes are from
@@ -55,7 +56,7 @@ func getLongDescriptionForGopkg(gopkg string) (string, error) {
5556
cmd.Stdin = bytes.NewBuffer(output)
5657
out, err := cmd.Output()
5758
if err != nil {
58-
return "", err
59+
return "", fmt.Errorf("fmt: %w", err)
5960
}
6061
return reformatForControl(strings.TrimSpace(string(out))), nil
6162
}

estimate.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,34 @@ func removeVendor(gopath string) (found bool, _ error) {
5050
}
5151
found = true
5252
if err := os.RemoveAll(path); err != nil {
53-
return err
53+
return fmt.Errorf("remove all: %w", err)
5454
}
5555
return filepath.SkipDir
5656
})
57-
return found, err
57+
return found, fmt.Errorf("walk: %w", err)
5858
}
5959

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

6868
if err := get(gopath, importpath); err != nil {
69-
return err
69+
return fmt.Errorf("go get: %w", err)
7070
}
7171

7272
found, err := removeVendor(gopath)
7373
if err != nil {
74-
return err
74+
return fmt.Errorf("remove vendor: %w", err)
7575
}
7676

7777
if found {
7878
// Fetch un-vendored dependencies
7979
if err := get(gopath, importpath); err != nil {
80-
return err
80+
return fmt.Errorf("fetch un-vendored: go get: %w", err)
8181
}
8282
}
8383

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

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

188188
err := fs.Parse(args)
189189
if err != nil {
190-
log.Fatal(err)
190+
log.Fatalf("parse args: %s", err)
191191
}
192192

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

200200
if err := estimate(fs.Arg(0)); err != nil {
201-
log.Fatal(err)
201+
log.Fatalf("estimate: %s", err)
202202
}
203203
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/Debian/dh-make-golang
22

3-
go 1.12
3+
go 1.13
44

55
require (
66
github.com/google/go-github/v38 v38.1.0

0 commit comments

Comments
 (0)