Skip to content

Commit

Permalink
Add optional error message to the error check
Browse files Browse the repository at this point in the history
  • Loading branch information
HikariKnight committed Apr 7, 2023
1 parent 5cbc041 commit e1ca657
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 15 additions & 3 deletions pkg/errorcheck/errorcheck.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package errorcheck

import "log"
import (
"log"
)

func ErrorCheck(err error) {
func ErrorCheck(err error, message ...string) {
if err != nil {
log.Fatal(err.Error())
if len(message) != 0 {
// For each message we print a new line to stderr
for _, v := range message {
log.Println(v)
}
// Then write the exit code and exits
log.Fatal(err)
} else {
// Write the exit code and exit
log.Fatal(err)
}
}
}
6 changes: 3 additions & 3 deletions pkg/iommu/iommu.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (i *IOMMU) Read() {
i.Groups = make(map[int]*Group)
// Get all groups and associated devices
iommu_devices, err := filepath.Glob("/sys/kernel/iommu_groups/*/devices/*")
errorcheck.ErrorCheck(err)
errorcheck.ErrorCheck(err, "Unable to glob /sys/kernel/iommu_groups/*/devices/*")
pci, err := ghw.PCI(ghw.WithDisableWarnings())
errorcheck.ErrorCheck(err)
errorcheck.ErrorCheck(err, "Failed to parse PCI devices")

// Regex to get IOMMU groups and their devices from filepath
iommu_regex := regexp.MustCompile(`/sys/kernel/iommu_groups/(.*)/devices/(.*)`)
Expand Down Expand Up @@ -345,7 +345,7 @@ func MatchDEVs(regex string, pArg *params.Params) []string {

output := GetAllDevices(pArg)
gpuReg, err := regexp.Compile(regex)
errorcheck.ErrorCheck(err)
errorcheck.ErrorCheck(err, "Failed to compile MatchDEVs regex")

for _, line := range output {
if gpuReg.MatchString(line) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/iommu/rom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func GetRomPath(device *pci.Device, pArg *params.Params) []string {

// Walk through /sys/devices/ and add all paths that has a rom file and matches the device.Address to the roms variable
err := filepath.Walk("/sys/devices/", func(path string, info fs.FileInfo, err error) error {
errorcheck.ErrorCheck(err)
errorcheck.ErrorCheck(err, "Unable to walk /sys/devices/")

// If the file name is "rom" and the path contains the PCI address for our device
if info.Name() == "rom" && strings.Contains(path, device.Address) {
Expand All @@ -27,7 +27,7 @@ func GetRomPath(device *pci.Device, pArg *params.Params) []string {
}
return nil
})
errorcheck.ErrorCheck(err)
errorcheck.ErrorCheck(err, "Failed to walk /sys/devices/ to find the device rom")

// Return all found rom files
return roms
Expand Down

0 comments on commit e1ca657

Please sign in to comment.