Skip to content

Commit ac44f90

Browse files
committed
Update error message handling
Use Go 1.13 `%w` error formatting * Update minimum Go version to 1.13. * Bump vendoring. * Update all error format strings to use %w. * Cleanup consistency of use. * Cleanup use of `errors.New("string" + var)` to use `fmt.Errorf()`. #310 Signed-off-by: Ben Kochie <superq@gmail.com>
1 parent d335eb2 commit ac44f90

38 files changed

+133
-147
lines changed

arp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type ARPEntry struct {
3636
func (fs FS) GatherARPEntries() ([]ARPEntry, error) {
3737
data, err := ioutil.ReadFile(fs.proc.Path("net/arp"))
3838
if err != nil {
39-
return nil, fmt.Errorf("error reading arp %s: %s", fs.proc.Path("net/arp"), err)
39+
return nil, fmt.Errorf("error reading arp %s: %w", fs.proc.Path("net/arp"), err)
4040
}
4141

4242
return parseARPEntries(data)
@@ -59,7 +59,7 @@ func parseARPEntries(data []byte) ([]ARPEntry, error) {
5959
} else if width == expectedDataWidth {
6060
entry, err := parseARPEntry(columns)
6161
if err != nil {
62-
return []ARPEntry{}, fmt.Errorf("failed to parse ARP entry: %s", err)
62+
return []ARPEntry{}, fmt.Errorf("failed to parse ARP entry: %w", err)
6363
}
6464
entries = append(entries, entry)
6565
} else {

bcache/get.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,12 @@ func (p *parser) getPriorityStats() PriorityStats {
329329
for scanner.Scan() {
330330
err = parsePriorityStats(scanner.Text(), &res)
331331
if err != nil {
332-
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
332+
p.err = fmt.Errorf("failed to parse path %s: %w", path, err)
333333
return res
334334
}
335335
}
336336
if err := scanner.Err(); err != nil {
337-
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
337+
p.err = fmt.Errorf("failed to parse path %s: %w", path, err)
338338
return res
339339
}
340340
return res
@@ -358,12 +358,12 @@ func (p *parser) getWritebackRateDebug() WritebackRateDebugStats {
358358
for scanner.Scan() {
359359
err = parseWritebackRateDebug(scanner.Text(), &res)
360360
if err != nil {
361-
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
361+
p.err = fmt.Errorf("failed to parse path %s: %w", path, err)
362362
return res
363363
}
364364
}
365365
if err := scanner.Err(); err != nil {
366-
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
366+
p.err = fmt.Errorf("failed to parse path %s: %w", path, err)
367367
return res
368368
}
369369
return res

buddyinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) {
7474
for i := 0; i < arraySize; i++ {
7575
sizes[i], err = strconv.ParseFloat(parts[i+4], 64)
7676
if err != nil {
77-
return nil, fmt.Errorf("invalid value in buddyinfo: %s", err)
77+
return nil, fmt.Errorf("invalid value in buddyinfo: %w", err)
7878
}
7979
}
8080

cpuinfo.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package procfs
1818
import (
1919
"bufio"
2020
"bytes"
21-
"errors"
21+
"fmt"
2222
"regexp"
2323
"strconv"
2424
"strings"
@@ -77,7 +77,7 @@ func parseCPUInfoX86(info []byte) ([]CPUInfo, error) {
7777
// find the first "processor" line
7878
firstLine := firstNonEmptyLine(scanner)
7979
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
80-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
80+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
8181
}
8282
field := strings.SplitN(firstLine, ": ", 2)
8383
v, err := strconv.ParseUint(field[1], 0, 32)
@@ -192,7 +192,7 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
192192
firstLine := firstNonEmptyLine(scanner)
193193
match, _ := regexp.MatchString("^[Pp]rocessor", firstLine)
194194
if !match || !strings.Contains(firstLine, ":") {
195-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
195+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
196196
}
197197
field := strings.SplitN(firstLine, ": ", 2)
198198
cpuinfo := []CPUInfo{}
@@ -256,7 +256,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
256256

257257
firstLine := firstNonEmptyLine(scanner)
258258
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
259-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
259+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
260260
}
261261
field := strings.SplitN(firstLine, ": ", 2)
262262
cpuinfo := []CPUInfo{}
@@ -281,7 +281,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
281281
if strings.HasPrefix(line, "processor") {
282282
match := cpuinfoS390XProcessorRegexp.FindStringSubmatch(line)
283283
if len(match) < 2 {
284-
return nil, errors.New("Invalid line found in cpuinfo: " + line)
284+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
285285
}
286286
cpu := commonCPUInfo
287287
v, err := strconv.ParseUint(match[1], 0, 32)
@@ -341,7 +341,7 @@ func parseCPUInfoMips(info []byte) ([]CPUInfo, error) {
341341
// find the first "processor" line
342342
firstLine := firstNonEmptyLine(scanner)
343343
if !strings.HasPrefix(firstLine, "system type") || !strings.Contains(firstLine, ":") {
344-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
344+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
345345
}
346346
field := strings.SplitN(firstLine, ": ", 2)
347347
cpuinfo := []CPUInfo{}
@@ -383,7 +383,7 @@ func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) {
383383

384384
firstLine := firstNonEmptyLine(scanner)
385385
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
386-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
386+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
387387
}
388388
field := strings.SplitN(firstLine, ": ", 2)
389389
v, err := strconv.ParseUint(field[1], 0, 32)
@@ -428,7 +428,7 @@ func parseCPUInfoRISCV(info []byte) ([]CPUInfo, error) {
428428

429429
firstLine := firstNonEmptyLine(scanner)
430430
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
431-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
431+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
432432
}
433433
field := strings.SplitN(firstLine, ": ", 2)
434434
v, err := strconv.ParseUint(field[1], 0, 32)
@@ -464,7 +464,7 @@ func parseCPUInfoRISCV(info []byte) ([]CPUInfo, error) {
464464
}
465465

466466
func parseCPUInfoDummy(_ []byte) ([]CPUInfo, error) { // nolint:unused,deadcode
467-
return nil, errors.New("not implemented")
467+
return nil, fmt.Errorf("not implemented")
468468
}
469469

470470
// firstNonEmptyLine advances the scanner to the first non-empty line

crypto.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ func (fs FS) Crypto() ([]Crypto, error) {
5555
path := fs.proc.Path("crypto")
5656
b, err := util.ReadFileNoStat(path)
5757
if err != nil {
58-
return nil, fmt.Errorf("error reading crypto %s: %s", path, err)
58+
return nil, fmt.Errorf("error reading crypto %s: %w", path, err)
5959
}
6060

6161
crypto, err := parseCrypto(bytes.NewReader(b))
6262
if err != nil {
63-
return nil, fmt.Errorf("error parsing crypto %s: %s", path, err)
63+
return nil, fmt.Errorf("error parsing crypto %s: %w", path, err)
6464
}
6565

6666
return crypto, nil

fscache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (fs FS) Fscacheinfo() (Fscacheinfo, error) {
236236

237237
m, err := parseFscacheinfo(bytes.NewReader(b))
238238
if err != nil {
239-
return Fscacheinfo{}, fmt.Errorf("failed to parse Fscacheinfo: %v", err)
239+
return Fscacheinfo{}, fmt.Errorf("failed to parse Fscacheinfo: %w", err)
240240
}
241241

242242
return *m, nil

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module github.com/prometheus/procfs
22

3-
go 1.12
3+
go 1.13
44

55
require (
6-
github.com/google/go-cmp v0.3.1
7-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
8-
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
6+
github.com/google/go-cmp v0.5.4
7+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
8+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
99
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
2-
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
3-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
4-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5-
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
6-
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1+
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
2+
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
4+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
6+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
8+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/fs/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type FS string
3939
func NewFS(mountPoint string) (FS, error) {
4040
info, err := os.Stat(mountPoint)
4141
if err != nil {
42-
return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
42+
return "", fmt.Errorf("could not read %s: %w", mountPoint, err)
4343
}
4444
if !info.IsDir() {
4545
return "", fmt.Errorf("mount point %s is not a directory", mountPoint)

iscsi/get.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func GetStats(iqnPath string) (*Stats, error) {
3636

3737
matches, err := filepath.Glob(filepath.Join(iqnPath, "tpgt*"))
3838
if err != nil {
39-
return nil, fmt.Errorf("iscsi: GetStats: get TPGT path error %v", err)
39+
return nil, fmt.Errorf("iscsi: GetStats: get TPGT path error %w", err)
4040
}
4141
istats.Tpgt = make([]TPGT, len(matches))
4242

@@ -63,11 +63,11 @@ func GetStats(iqnPath string) (*Stats, error) {
6363
func isPathEnable(path string) (bool, error) {
6464
enableReadout, err := ioutil.ReadFile(filepath.Join(path, "enable"))
6565
if err != nil {
66-
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %v", err)
66+
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %w", err)
6767
}
6868
isEnable, err := strconv.ParseBool(strings.TrimSpace(string(enableReadout)))
6969
if err != nil {
70-
return false, fmt.Errorf("iscsi: isPathEnable ParseBool error %v", err)
70+
return false, fmt.Errorf("iscsi: isPathEnable ParseBool error %w", err)
7171
}
7272
return isEnable, nil
7373
}
@@ -77,14 +77,14 @@ func getLunLinkTarget(lunPath string) (lunObject LUN, err error) {
7777
lunObject.LunPath = lunPath
7878
files, err := ioutil.ReadDir(lunPath)
7979
if err != nil {
80-
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %s error %v", lunPath, err)
80+
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %s: %w", lunPath, err)
8181
}
8282
for _, file := range files {
8383
fileInfo, _ := os.Lstat(filepath.Join(lunPath, file.Name()))
8484
if fileInfo.Mode()&os.ModeSymlink != 0 {
8585
target, err := os.Readlink(filepath.Join(lunPath, fileInfo.Name()))
8686
if err != nil {
87-
return lunObject, fmt.Errorf("getLunLinkTarget: Readlink err %v", err)
87+
return lunObject, fmt.Errorf("getLunLinkTarget: Readlink: %w", err)
8888
}
8989
targetPath, objectName := filepath.Split(target)
9090
_, typeWithNumber := filepath.Split(filepath.Clean(targetPath))
@@ -112,24 +112,21 @@ func ReadWriteOPS(iqnPath string, tpgt string, lun string) (readmb uint64,
112112
"statistics/scsi_tgt_port/read_mbytes")
113113
readmb, err = util.ReadUintFromFile(readmbPath)
114114
if err != nil {
115-
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: read_mbytes error file %s and %v",
116-
readmbPath, err)
115+
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: read_mbytes error file %s: %w", readmbPath, err)
117116
}
118117

119118
writembPath := filepath.Join(iqnPath, tpgt, "lun", lun,
120119
"statistics/scsi_tgt_port/write_mbytes")
121120
writemb, err = util.ReadUintFromFile(writembPath)
122121
if err != nil {
123-
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: write_mbytes error file %s and %v",
124-
writembPath, err)
122+
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: write_mbytes error file %s: %w", writembPath, err)
125123
}
126124

127125
iopsPath := filepath.Join(iqnPath, tpgt, "lun", lun,
128126
"statistics/scsi_tgt_port/in_cmds")
129127
iops, err = util.ReadUintFromFile(iopsPath)
130128
if err != nil {
131-
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: in_cmds error file %s and %v",
132-
iopsPath, err)
129+
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: in_cmds error file %s: %w", iopsPath, err)
133130
}
134131

135132
return readmb, writemb, iops, nil
@@ -238,7 +235,7 @@ func (fs FS) GetRDMCPPath(rdmcpNumber string, objectName string) (*RDMCP, error)
238235
}
239236
isEnable, err := isPathEnable(rdmcpPath)
240237
if err != nil {
241-
return nil, fmt.Errorf("iscsi: GetRDMCPPath: error %v", err)
238+
return nil, fmt.Errorf("iscsi: GetRDMCPPath: error %w", err)
242239
}
243240
if isEnable {
244241
return &rdmcp, nil

0 commit comments

Comments
 (0)