Skip to content

Commit a52d3fb

Browse files
committed
refactor: use github.com/pkg/errors (fix #240)
* Use `github.com/pkg/errors` instead of `errors` * Use `errors.Wrap()` instead of constructing error message by `fmt.Errorf()` or string concatenation * Use `errors.Errorf()` instead of `fmt.Errorf()`
1 parent edbce38 commit a52d3fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2033
-113
lines changed

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package config
22

33
import (
4-
"fmt"
5-
64
"github.com/BurntSushi/toml"
5+
"github.com/pkg/errors"
6+
77
"github.com/vim-volt/volt/pathutil"
88
)
99

@@ -80,7 +80,7 @@ func merge(cfg, initCfg *Config) {
8080

8181
func validate(cfg *Config) error {
8282
if cfg.Build.Strategy != "symlink" && cfg.Build.Strategy != "copy" {
83-
return fmt.Errorf("build.strategy is %q: valid values are %q or %q", cfg.Build.Strategy, "symlink", "copy")
83+
return errors.Errorf("build.strategy is %q: valid values are %q or %q", cfg.Build.Strategy, "symlink", "copy")
8484
}
8585
return nil
8686
}

fileutil/copyfile_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
package fileutil
3232

3333
import (
34-
"fmt"
3534
"os"
3635
"syscall"
36+
37+
"github.com/pkg/errors"
3738
)
3839

3940
// CopyFile copies the contents of the file named src to the file named
@@ -63,7 +64,7 @@ func CopyFile(src, dst string, buf []byte, perm os.FileMode) error {
6364
for {
6465
n, err := syscall.Sendfile(wfd, rfd, nil, readsize)
6566
if err != nil {
66-
return fmt.Errorf("sendfile(%q, %q) failed: %s", src, dst, err.Error())
67+
return errors.Errorf("sendfile(%q, %q) failed: %s", src, dst, err.Error())
6768
}
6869
written += int64(n)
6970
if written >= fi.Size() {
@@ -72,7 +73,7 @@ func CopyFile(src, dst string, buf []byte, perm os.FileMode) error {
7273
}
7374
} else {
7475
if _, err := syscall.Sendfile(wfd, rfd, nil, int(fi.Size())); err != nil {
75-
return fmt.Errorf("sendfile(%q, %q) failed: %s", src, dst, err.Error())
76+
return errors.Errorf("sendfile(%q, %q) failed: %s", src, dst, err.Error())
7677
}
7778
}
7879
return nil

gitutil/gitutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package gitutil
22

33
import (
4-
"errors"
5-
"fmt"
64
"regexp"
75

6+
"github.com/pkg/errors"
7+
88
"github.com/vim-volt/volt/pathutil"
99
git "gopkg.in/src-d/go-git.v4"
1010
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -109,7 +109,7 @@ func GetUpstreamRemote(r *git.Repository) (string, error) {
109109
subsec := cfg.Raw.Section("branch").Subsection(branch[1])
110110
remote := subsec.Option("remote")
111111
if remote == "" {
112-
return "", fmt.Errorf("gitconfig 'branch.%s.remote' is not found", subsec.Name)
112+
return "", errors.Errorf("gitconfig 'branch.%s.remote' is not found", subsec.Name)
113113
}
114114
return remote, nil
115115
}

httputil/httputil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package httputil
22

33
import (
4-
"errors"
4+
"github.com/pkg/errors"
55
"io"
66
"io/ioutil"
77
"net/http"

internal/testutil/testutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package testutil
22

33
import (
4-
"errors"
54
"fmt"
5+
"github.com/pkg/errors"
66
"io/ioutil"
77
"os"
88
"os/exec"

lockjson/lockjson.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package lockjson
22

33
import (
44
"encoding/json"
5-
"errors"
6-
"fmt"
75
"io/ioutil"
86
"os"
97
"path/filepath"
108
"strconv"
119

10+
"github.com/pkg/errors"
11+
1212
"github.com/vim-volt/volt/logger"
1313
"github.com/vim-volt/volt/pathutil"
1414
)
@@ -112,19 +112,19 @@ func read(doLog bool) (*LockJSON, error) {
112112
// Validate lock.json
113113
err = validate(&lockJSON)
114114
if err != nil {
115-
return nil, errors.New("validation failed: lock.json: " + err.Error())
115+
return nil, errors.Wrap(err, "validation failed: lock.json")
116116
}
117117

118118
return &lockJSON, nil
119119
}
120120

121121
func validate(lockJSON *LockJSON) error {
122122
if lockJSON.Version < 1 {
123-
return fmt.Errorf("lock.json version is '%d' (must be 1 or greater)", lockJSON.Version)
123+
return errors.Errorf("lock.json version is '%d' (must be 1 or greater)", lockJSON.Version)
124124
}
125125
// Validate if volt can manipulate lock.json of this version
126126
if lockJSON.Version > lockJSONVersion {
127-
return fmt.Errorf("this lock.json version is '%d' which volt cannot recognize. please upgrade volt to process this file", lockJSON.Version)
127+
return errors.Errorf("this lock.json version is '%d' which volt cannot recognize. please upgrade volt to process this file", lockJSON.Version)
128128
}
129129

130130
// Validate if missing required keys exist

pathutil/pathutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pathutil
22

33
import (
4-
"errors"
4+
"github.com/pkg/errors"
55
"os"
66
"os/exec"
77
"path/filepath"

plugconf/plugconf.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package plugconf
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io/ioutil"
98
"path"
@@ -12,6 +11,8 @@ import (
1211
"sort"
1312
"strings"
1413

14+
"github.com/pkg/errors"
15+
1516
multierror "github.com/hashicorp/go-multierror"
1617
"github.com/vim-volt/volt/httputil"
1718
"github.com/vim-volt/volt/lockjson"
@@ -392,7 +393,7 @@ func ParsePlugconf(file *ast.File, src []byte, path string) (*ParsedInfo, *Parse
392393
}
393394
case isProhibitedFuncName(ident.Name):
394395
parseErr.merr = multierror.Append(parseErr.merr,
395-
fmt.Errorf(
396+
errors.Errorf(
396397
"'%s' is prohibited function name. please use other function name", ident.Name))
397398
default:
398399
functions = append(functions, string(extractBody(fn, src)))

subcmd/builder/base.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package builder
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"os"
87
"os/exec"
98
"path/filepath"
109
"strings"
1110

11+
"github.com/pkg/errors"
12+
1213
"github.com/hashicorp/go-multierror"
1314
"github.com/vim-volt/volt/fileutil"
1415
"github.com/vim-volt/volt/lockjson"
@@ -79,7 +80,7 @@ func (builder *BaseBuilder) installRCFile(profileName, srcRCFileName, dst string
7980
if !pathutil.Exists(src) {
8081
return nil
8182
}
82-
return fmt.Errorf("'%s' is not an auto-generated file. please move to '%s' and re-run 'volt build'", dst, pathutil.RCDir(profileName))
83+
return errors.Errorf("'%s' is not an auto-generated file. please move to '%s' and re-run 'volt build'", dst, pathutil.RCDir(profileName))
8384
}
8485
}
8586

@@ -175,7 +176,7 @@ func (builder *BaseBuilder) helptags(reposPath pathutil.ReposPath, vimExePath st
175176
logger.Debugf("Executing '%s %s' ...", vimExePath, strings.Join(vimArgs, " "))
176177
err := exec.Command(vimExePath, vimArgs...).Run()
177178
if err != nil {
178-
return errors.New("failed to make tags file: " + err.Error())
179+
return errors.Wrap(err, "failed to make tags file")
179180
}
180181
return nil
181182
}

0 commit comments

Comments
 (0)