Return errors, rather than printing them

This commit is contained in:
2025-05-30 01:08:43 +00:00
parent 74b8261bc8
commit 40f0f35f45
21 changed files with 253 additions and 183 deletions
+10 -12
View File
@@ -1,36 +1,34 @@
package global
import (
"errors"
"io/fs"
"os"
"github.com/rwinkhart/go-boilerplate/back"
)
// GetOldDeviceID returns the current device ID or
// FSMisc if there is no device ID (e.g. first run).
func GetCurrentDeviceID() string {
deviceIDList := GenDeviceIDList(false) // errorOnFail is false so that nil is received when the devices directory does not exist
func GetCurrentDeviceID() (string, error) {
deviceIDList, err := GenDeviceIDList()
if err != nil {
return "", errors.New("unable to generate device ID list: " + err.Error())
}
var deviceID string
if len(deviceIDList) > 0 {
deviceID = (deviceIDList)[0].Name()
} else {
deviceID = FSMisc // indicates to server that no device ID is being replaced
}
return deviceID
return deviceID, nil
}
// GenDeviceIDList returns a slice of all registered device IDs.
// Requires: errorOnFail (set to true to throw an error if the devices directory cannot be read/does not exist)
func GenDeviceIDList(errorOnFail bool) []fs.DirEntry {
func GenDeviceIDList() ([]fs.DirEntry, error) {
// create a slice of all registered devices
deviceIDList, err := os.ReadDir(ConfigDir + PathSeparator + "devices")
if err != nil {
if errorOnFail {
back.PrintError("Failed to read the devices directory: "+err.Error(), back.ErrorRead, true)
} else {
return nil // a nil return value indicates that the devices directory could not be read/does not exist
}
return nil, errors.New("unable to read the devices directory: " + err.Error())
}
return deviceIDList
return deviceIDList, nil
}
+10 -6
View File
@@ -1,6 +1,7 @@
package global
import (
"errors"
"os"
"github.com/rwinkhart/go-boilerplate/back"
@@ -8,15 +9,18 @@ import (
// DirInit creates the libmutton directories.
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
func DirInit(preserveOldConfigDir bool) string {
func DirInit(preserveOldConfigDir bool) (string, error) {
// create EntryRoot
err := os.MkdirAll(EntryRoot, 0700)
if err != nil {
back.PrintError("Failed to create \""+EntryRoot+"\": "+err.Error(), back.ErrorWrite, true)
return "", errors.New("unable to create \"" + EntryRoot + "\": " + err.Error())
}
// get old device ID before its potential removal
oldDeviceID := GetCurrentDeviceID()
oldDeviceID, err := GetCurrentDeviceID()
if err != nil {
return "", errors.New("unable to get current device ID: " + err.Error())
}
// remove existing config directory (if it exists and not in append mode)
if !preserveOldConfigDir {
@@ -24,7 +28,7 @@ func DirInit(preserveOldConfigDir bool) string {
if isAccessible {
err = os.RemoveAll(ConfigDir)
if err != nil {
back.PrintError("Failed to remove existing config directory: "+err.Error(), back.ErrorWrite, true)
return "", errors.New("unable to remove existing config directory: " + err.Error())
}
}
}
@@ -32,8 +36,8 @@ func DirInit(preserveOldConfigDir bool) string {
// create config directory w/devices subdirectory
err = os.MkdirAll(ConfigDir+PathSeparator+"devices", 0700)
if err != nil {
back.PrintError("Failed to create \""+ConfigDir+"\": "+err.Error(), back.ErrorWrite, true)
return "", errors.New("unable to create \"" + ConfigDir + "\": " + err.Error())
}
return oldDeviceID
return oldDeviceID, nil
}