cmd/root: Let the callers log the errors when replacing them

The errors should be propagated up the call chain either verbatim or by
wrapping them with all relevant context when necessary (as long as they
don't violate the API boundaries).

The errors should be logged only when there's a break in the upward
propagation, either because they need to be reformatted before being
shown to the user or because they would expose implementation details
that aren't part of the API contract.  Not logging the errors in such
cases might make it difficult to debug problems later on.

https://github.com/containers/toolbox/pull/1202
This commit is contained in:
Debarshi Ray 2022-12-16 01:03:24 +01:00
parent 2d1eff8f12
commit 21ae296ad0

View file

@ -145,10 +145,12 @@ func preRun(cmd *cobra.Command, args []string) error {
currentUser.Username)
if _, err := validateSubIDFile("/etc/subuid"); err != nil {
logrus.Debugf("Checking sub-ID file /etc/subuid: %s", err)
return newSubIDFileError()
}
if _, err := validateSubIDFile("/etc/subgid"); err != nil {
logrus.Debugf("Checking sub-ID file /etc/subgid: %s", err)
return newSubIDFileError()
}
}
@ -401,8 +403,7 @@ func validateSubIDFile(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
logrus.Debugf("Validating sub-ID file: failed to open %s: %s", path, err)
return false, fmt.Errorf("failed to open %s", path)
return false, fmt.Errorf("failed to open: %w", err)
}
scanner := bufio.NewScanner(file)
@ -419,5 +420,5 @@ func validateSubIDFile(path string) (bool, error) {
}
}
return false, fmt.Errorf("failed to find an entry for user %s in %s", currentUser.Username, path)
return false, fmt.Errorf("failed to find an entry for user %s", currentUser.Username)
}