mirror of
https://github.com/containers/podman.git
synced 2025-06-15 13:48:29 +08:00

Continue the refactoring of image removal. I didn't manage to break all the following changes into smaller and easier to digest commits due to time constraints: * Return an error slice instead of a single error. Use multierror only in the client/frontend. Reflect that in the types. * Use the batch image removal in the client while preserving the more rest-idiomatic single-image removal endpoint. * Add a new handler for the single-image removal endpoint to make it share the same code as the batch endpoint. * Expose bindings for the single and batch endpoints, so we can properly test them. * Add several convenience functions for error handling to pkg/errorhandling. * Set the correct error type in libpod to set the exit code to 2 when one or more containers are using an image. * Massage the bindings tests a bit and tackle compilation errors. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package errorhandling
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// JoinErrors converts the error slice into a single human-readable error.
|
|
func JoinErrors(errs []error) error {
|
|
if len(errs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// `multierror` appends new lines which we need to remove to prevent
|
|
// blank lines when printing the error.
|
|
var multiE *multierror.Error
|
|
multiE = multierror.Append(multiE, errs...)
|
|
return errors.New(strings.TrimSpace(multiE.ErrorOrNil().Error()))
|
|
}
|
|
|
|
// ErrorsToString converts the slice of errors into a slice of corresponding
|
|
// error messages.
|
|
func ErrorsToStrings(errs []error) []string {
|
|
strErrs := make([]string, len(errs))
|
|
for i := range errs {
|
|
strErrs[i] = errs[i].Error()
|
|
}
|
|
return strErrs
|
|
}
|
|
|
|
// StringsToErrors converts a slice of error messages into a slice of
|
|
// corresponding errors.
|
|
func StringsToErrors(strErrs []string) []error {
|
|
errs := make([]error, len(strErrs))
|
|
for i := range strErrs {
|
|
errs[i] = errors.New(strErrs[i])
|
|
}
|
|
return errs
|
|
}
|
|
|
|
// SyncQuiet syncs a file and logs any error. Should only be used within
|
|
// a defer.
|
|
func SyncQuiet(f *os.File) {
|
|
if err := f.Sync(); err != nil {
|
|
logrus.Errorf("unable to sync file %s: %q", f.Name(), err)
|
|
}
|
|
}
|
|
|
|
// CloseQuiet closes a file and logs any error. Should only be used within
|
|
// a defer.
|
|
func CloseQuiet(f *os.File) {
|
|
if err := f.Close(); err != nil {
|
|
logrus.Errorf("unable to close file %s: %q", f.Name(), err)
|
|
}
|
|
}
|