Skip to content

Commit b825ab5

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 b825ab5

38 files changed

+140
-153
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 %q: %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 %q: %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 %q: %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 %q: %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 %q: %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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"bufio"
2020
"bytes"
2121
"errors"
22+
"fmt"
2223
"regexp"
2324
"strconv"
2425
"strings"
@@ -77,7 +78,7 @@ func parseCPUInfoX86(info []byte) ([]CPUInfo, error) {
7778
// find the first "processor" line
7879
firstLine := firstNonEmptyLine(scanner)
7980
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
80-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
81+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
8182
}
8283
field := strings.SplitN(firstLine, ": ", 2)
8384
v, err := strconv.ParseUint(field[1], 0, 32)
@@ -192,7 +193,7 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
192193
firstLine := firstNonEmptyLine(scanner)
193194
match, _ := regexp.MatchString("^[Pp]rocessor", firstLine)
194195
if !match || !strings.Contains(firstLine, ":") {
195-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
196+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
196197
}
197198
field := strings.SplitN(firstLine, ": ", 2)
198199
cpuinfo := []CPUInfo{}
@@ -256,7 +257,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
256257

257258
firstLine := firstNonEmptyLine(scanner)
258259
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
259-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
260+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
260261
}
261262
field := strings.SplitN(firstLine, ": ", 2)
262263
cpuinfo := []CPUInfo{}
@@ -281,7 +282,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
281282
if strings.HasPrefix(line, "processor") {
282283
match := cpuinfoS390XProcessorRegexp.FindStringSubmatch(line)
283284
if len(match) < 2 {
284-
return nil, errors.New("Invalid line found in cpuinfo: " + line)
285+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
285286
}
286287
cpu := commonCPUInfo
287288
v, err := strconv.ParseUint(match[1], 0, 32)
@@ -341,7 +342,7 @@ func parseCPUInfoMips(info []byte) ([]CPUInfo, error) {
341342
// find the first "processor" line
342343
firstLine := firstNonEmptyLine(scanner)
343344
if !strings.HasPrefix(firstLine, "system type") || !strings.Contains(firstLine, ":") {
344-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
345+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
345346
}
346347
field := strings.SplitN(firstLine, ": ", 2)
347348
cpuinfo := []CPUInfo{}
@@ -383,7 +384,7 @@ func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) {
383384

384385
firstLine := firstNonEmptyLine(scanner)
385386
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
386-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
387+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
387388
}
388389
field := strings.SplitN(firstLine, ": ", 2)
389390
v, err := strconv.ParseUint(field[1], 0, 32)
@@ -428,7 +429,7 @@ func parseCPUInfoRISCV(info []byte) ([]CPUInfo, error) {
428429

429430
firstLine := firstNonEmptyLine(scanner)
430431
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
431-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
432+
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
432433
}
433434
field := strings.SplitN(firstLine, ": ", 2)
434435
v, err := strconv.ParseUint(field[1], 0, 32)

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 %q: %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 %q: %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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ 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 %q: %w", mountPoint, err)
4343
}
4444
if !info.IsDir() {
45-
return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
45+
return "", fmt.Errorf("mount point %q is not a directory", mountPoint)
4646
}
4747

4848
return FS(mountPoint), nil

iscsi/get.go

Lines changed: 12 additions & 15 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 %q: %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 %q: %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 %q: %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 %q: %w", iopsPath, err)
133130
}
134131

135132
return readmb, writemb, iops, nil
@@ -150,7 +147,7 @@ func (fs FS) GetFileioUdev(fileioNumber string, objectName string) (*FILEIO, err
150147
}
151148
filename, err := ioutil.ReadFile(udevPath)
152149
if err != nil {
153-
return nil, fmt.Errorf("iscsi: GetFileioUdev: Cannot read filename from udev link :%s", udevPath)
150+
return nil, fmt.Errorf("iscsi: GetFileioUdev: Cannot read filename from udev link %q", udevPath)
154151
}
155152
fileio.Filename = strings.TrimSpace(string(filename))
156153

@@ -172,7 +169,7 @@ func (fs FS) GetIblockUdev(iblockNumber string, objectName string) (*IBLOCK, err
172169
}
173170
filename, err := ioutil.ReadFile(udevPath)
174171
if err != nil {
175-
return nil, fmt.Errorf("iscsi: GetIBlockUdev: Cannot read iblock from udev link :%s", udevPath)
172+
return nil, fmt.Errorf("iscsi: GetIBlockUdev: Cannot read iblock from udev link %s", udevPath)
176173
}
177174
iblock.Iblock = strings.TrimSpace(string(filename))
178175

@@ -234,11 +231,11 @@ func (fs FS) GetRDMCPPath(rdmcpNumber string, objectName string) (*RDMCP, error)
234231
rdmcpPath := fs.configfs.Path(targetCore, rdmcp.Name, rdmcp.ObjectName)
235232

236233
if _, err := os.Stat(rdmcpPath); os.IsNotExist(err) {
237-
return nil, fmt.Errorf("iscsi: GetRDMCPPath: %s does not exist", rdmcpPath)
234+
return nil, fmt.Errorf("iscsi: GetRDMCPPath %q does not exist", rdmcpPath)
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)