Skip to content

Commit

Permalink
Merge pull request #357 from prometheus/superq/error_is
Browse files Browse the repository at this point in the history
Update error message handling
  • Loading branch information
SuperQ authored Feb 10, 2021
2 parents d335eb2 + 67a72b2 commit e971af5
Show file tree
Hide file tree
Showing 38 changed files with 140 additions and 153 deletions.
4 changes: 2 additions & 2 deletions arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ARPEntry struct {
func (fs FS) GatherARPEntries() ([]ARPEntry, error) {
data, err := ioutil.ReadFile(fs.proc.Path("net/arp"))
if err != nil {
return nil, fmt.Errorf("error reading arp %s: %s", fs.proc.Path("net/arp"), err)
return nil, fmt.Errorf("error reading arp %q: %w", fs.proc.Path("net/arp"), err)
}

return parseARPEntries(data)
Expand All @@ -59,7 +59,7 @@ func parseARPEntries(data []byte) ([]ARPEntry, error) {
} else if width == expectedDataWidth {
entry, err := parseARPEntry(columns)
if err != nil {
return []ARPEntry{}, fmt.Errorf("failed to parse ARP entry: %s", err)
return []ARPEntry{}, fmt.Errorf("failed to parse ARP entry: %w", err)
}
entries = append(entries, entry)
} else {
Expand Down
8 changes: 4 additions & 4 deletions bcache/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ func (p *parser) getPriorityStats() PriorityStats {
for scanner.Scan() {
err = parsePriorityStats(scanner.Text(), &res)
if err != nil {
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
p.err = fmt.Errorf("failed to parse path %q: %w", path, err)
return res
}
}
if err := scanner.Err(); err != nil {
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
p.err = fmt.Errorf("failed to parse path %q: %w", path, err)
return res
}
return res
Expand All @@ -358,12 +358,12 @@ func (p *parser) getWritebackRateDebug() WritebackRateDebugStats {
for scanner.Scan() {
err = parseWritebackRateDebug(scanner.Text(), &res)
if err != nil {
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
p.err = fmt.Errorf("failed to parse path %q: %w", path, err)
return res
}
}
if err := scanner.Err(); err != nil {
p.err = fmt.Errorf("failed to parse: %s (%s)", path, err)
p.err = fmt.Errorf("failed to parse path %q: %w", path, err)
return res
}
return res
Expand Down
2 changes: 1 addition & 1 deletion buddyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) {
for i := 0; i < arraySize; i++ {
sizes[i], err = strconv.ParseFloat(parts[i+4], 64)
if err != nil {
return nil, fmt.Errorf("invalid value in buddyinfo: %s", err)
return nil, fmt.Errorf("invalid value in buddyinfo: %w", err)
}
}

Expand Down
15 changes: 8 additions & 7 deletions cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -77,7 +78,7 @@ func parseCPUInfoX86(info []byte) ([]CPUInfo, error) {
// find the first "processor" line
firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
v, err := strconv.ParseUint(field[1], 0, 32)
Expand Down Expand Up @@ -192,7 +193,7 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
firstLine := firstNonEmptyLine(scanner)
match, _ := regexp.MatchString("^[Pp]rocessor", firstLine)
if !match || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
cpuinfo := []CPUInfo{}
Expand Down Expand Up @@ -256,7 +257,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {

firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
cpuinfo := []CPUInfo{}
Expand All @@ -281,7 +282,7 @@ func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
if strings.HasPrefix(line, "processor") {
match := cpuinfoS390XProcessorRegexp.FindStringSubmatch(line)
if len(match) < 2 {
return nil, errors.New("Invalid line found in cpuinfo: " + line)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
cpu := commonCPUInfo
v, err := strconv.ParseUint(match[1], 0, 32)
Expand Down Expand Up @@ -341,7 +342,7 @@ func parseCPUInfoMips(info []byte) ([]CPUInfo, error) {
// find the first "processor" line
firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "system type") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
cpuinfo := []CPUInfo{}
Expand Down Expand Up @@ -383,7 +384,7 @@ func parseCPUInfoPPC(info []byte) ([]CPUInfo, error) {

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

firstLine := firstNonEmptyLine(scanner)
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
return nil, errors.New("invalid cpuinfo file: " + firstLine)
return nil, fmt.Errorf("invalid cpuinfo file: %q", firstLine)
}
field := strings.SplitN(firstLine, ": ", 2)
v, err := strconv.ParseUint(field[1], 0, 32)
Expand Down
4 changes: 2 additions & 2 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func (fs FS) Crypto() ([]Crypto, error) {
path := fs.proc.Path("crypto")
b, err := util.ReadFileNoStat(path)
if err != nil {
return nil, fmt.Errorf("error reading crypto %s: %s", path, err)
return nil, fmt.Errorf("error reading crypto %q: %w", path, err)
}

crypto, err := parseCrypto(bytes.NewReader(b))
if err != nil {
return nil, fmt.Errorf("error parsing crypto %s: %s", path, err)
return nil, fmt.Errorf("error parsing crypto %q: %w", path, err)
}

return crypto, nil
Expand Down
2 changes: 1 addition & 1 deletion fscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (fs FS) Fscacheinfo() (Fscacheinfo, error) {

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

return *m, nil
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/prometheus/procfs

go 1.12
go 1.13

require (
github.com/google/go-cmp v0.3.1
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
github.com/google/go-cmp v0.5.4
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4 changes: 2 additions & 2 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type FS string
func NewFS(mountPoint string) (FS, error) {
info, err := os.Stat(mountPoint)
if err != nil {
return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
return "", fmt.Errorf("could not read %q: %w", mountPoint, err)
}
if !info.IsDir() {
return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
return "", fmt.Errorf("mount point %q is not a directory", mountPoint)
}

return FS(mountPoint), nil
Expand Down
27 changes: 12 additions & 15 deletions iscsi/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetStats(iqnPath string) (*Stats, error) {

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

Expand All @@ -63,11 +63,11 @@ func GetStats(iqnPath string) (*Stats, error) {
func isPathEnable(path string) (bool, error) {
enableReadout, err := ioutil.ReadFile(filepath.Join(path, "enable"))
if err != nil {
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %v", err)
return false, fmt.Errorf("iscsi: isPathEnable ReadFile error %w", err)
}
isEnable, err := strconv.ParseBool(strings.TrimSpace(string(enableReadout)))
if err != nil {
return false, fmt.Errorf("iscsi: isPathEnable ParseBool error %v", err)
return false, fmt.Errorf("iscsi: isPathEnable ParseBool error %w", err)
}
return isEnable, nil
}
Expand All @@ -77,14 +77,14 @@ func getLunLinkTarget(lunPath string) (lunObject LUN, err error) {
lunObject.LunPath = lunPath
files, err := ioutil.ReadDir(lunPath)
if err != nil {
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %s error %v", lunPath, err)
return lunObject, fmt.Errorf("getLunLinkTarget: ReadDir path %q: %w", lunPath, err)
}
for _, file := range files {
fileInfo, _ := os.Lstat(filepath.Join(lunPath, file.Name()))
if fileInfo.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(filepath.Join(lunPath, fileInfo.Name()))
if err != nil {
return lunObject, fmt.Errorf("getLunLinkTarget: Readlink err %v", err)
return lunObject, fmt.Errorf("getLunLinkTarget: Readlink: %w", err)
}
targetPath, objectName := filepath.Split(target)
_, typeWithNumber := filepath.Split(filepath.Clean(targetPath))
Expand Down Expand Up @@ -112,24 +112,21 @@ func ReadWriteOPS(iqnPath string, tpgt string, lun string) (readmb uint64,
"statistics/scsi_tgt_port/read_mbytes")
readmb, err = util.ReadUintFromFile(readmbPath)
if err != nil {
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: read_mbytes error file %s and %v",
readmbPath, err)
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: read_mbytes error file %q: %w", readmbPath, err)
}

writembPath := filepath.Join(iqnPath, tpgt, "lun", lun,
"statistics/scsi_tgt_port/write_mbytes")
writemb, err = util.ReadUintFromFile(writembPath)
if err != nil {
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: write_mbytes error file %s and %v",
writembPath, err)
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: write_mbytes error file %q: %w", writembPath, err)
}

iopsPath := filepath.Join(iqnPath, tpgt, "lun", lun,
"statistics/scsi_tgt_port/in_cmds")
iops, err = util.ReadUintFromFile(iopsPath)
if err != nil {
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: in_cmds error file %s and %v",
iopsPath, err)
return 0, 0, 0, fmt.Errorf("iscsi: ReadWriteOPS: in_cmds error file %q: %w", iopsPath, err)
}

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

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

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

if _, err := os.Stat(rdmcpPath); os.IsNotExist(err) {
return nil, fmt.Errorf("iscsi: GetRDMCPPath: %s does not exist", rdmcpPath)
return nil, fmt.Errorf("iscsi: GetRDMCPPath %q does not exist", rdmcpPath)
}
isEnable, err := isPathEnable(rdmcpPath)
if err != nil {
return nil, fmt.Errorf("iscsi: GetRDMCPPath: error %v", err)
return nil, fmt.Errorf("iscsi: GetRDMCPPath: error %w", err)
}
if isEnable {
return &rdmcp, nil
Expand Down
4 changes: 2 additions & 2 deletions loadavg.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func parseLoad(loadavgBytes []byte) (*LoadAvg, error) {
loads := make([]float64, 3)
parts := strings.Fields(string(loadavgBytes))
if len(parts) < 3 {
return nil, fmt.Errorf("malformed loadavg line: too few fields in loadavg string: %s", string(loadavgBytes))
return nil, fmt.Errorf("malformed loadavg line: too few fields in loadavg string: %q", string(loadavgBytes))
}

var err error
for i, load := range parts[0:3] {
loads[i], err = strconv.ParseFloat(load, 64)
if err != nil {
return nil, fmt.Errorf("could not parse load '%s': %s", load, err)
return nil, fmt.Errorf("could not parse load %q: %w", load, err)
}
}
return &LoadAvg{
Expand Down
19 changes: 8 additions & 11 deletions mdstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (fs FS) MDStat() ([]MDStat, error) {
}
mdstat, err := parseMDStat(data)
if err != nil {
return nil, fmt.Errorf("error parsing mdstat %s: %s", fs.proc.Path("mdstat"), err)
return nil, fmt.Errorf("error parsing mdstat %q: %w", fs.proc.Path("mdstat"), err)
}
return mdstat, nil
}
Expand All @@ -85,10 +85,7 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
state := deviceFields[2] // active or inactive

if len(lines) <= i+3 {
return nil, fmt.Errorf(
"error parsing %s: too few lines for md device",
mdName,
)
return nil, fmt.Errorf("error parsing %q: too few lines for md device", mdName)
}

// Failed disks have the suffix (F) & Spare disks have the suffix (S).
Expand All @@ -97,7 +94,7 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
active, total, size, err := evalStatusLine(lines[i], lines[i+1])

if err != nil {
return nil, fmt.Errorf("error parsing md device lines: %s", err)
return nil, fmt.Errorf("error parsing md device lines: %w", err)
}

syncLineIdx := i + 2
Expand Down Expand Up @@ -129,7 +126,7 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
} else {
syncedBlocks, err = evalRecoveryLine(lines[syncLineIdx])
if err != nil {
return nil, fmt.Errorf("error parsing sync line in md device %s: %s", mdName, err)
return nil, fmt.Errorf("error parsing sync line in md device %q: %w", mdName, err)
}
}
}
Expand All @@ -155,7 +152,7 @@ func evalStatusLine(deviceLine, statusLine string) (active, total, size int64, e
sizeStr := strings.Fields(statusLine)[0]
size, err = strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
return 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
}

if strings.Contains(deviceLine, "raid0") || strings.Contains(deviceLine, "linear") {
Expand All @@ -175,12 +172,12 @@ func evalStatusLine(deviceLine, statusLine string) (active, total, size int64, e

total, err = strconv.ParseInt(matches[2], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
return 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
}

active, err = strconv.ParseInt(matches[3], 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
return 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
}

return active, total, size, nil
Expand All @@ -194,7 +191,7 @@ func evalRecoveryLine(recoveryLine string) (syncedBlocks int64, err error) {

syncedBlocks, err = strconv.ParseInt(matches[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("%s in recoveryLine: %s", err, recoveryLine)
return 0, fmt.Errorf("error parsing int from recoveryLine %q: %w", recoveryLine, err)
}

return syncedBlocks, nil
Expand Down
2 changes: 1 addition & 1 deletion meminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (fs FS) Meminfo() (Meminfo, error) {

m, err := parseMemInfo(bytes.NewReader(b))
if err != nil {
return Meminfo{}, fmt.Errorf("failed to parse meminfo: %v", err)
return Meminfo{}, fmt.Errorf("failed to parse meminfo: %w", err)
}

return *m, nil
Expand Down
Loading

0 comments on commit e971af5

Please sign in to comment.