lib/versioner: Improve error messages (fixes #7354) (#7357)

This commit is contained in:
Simon Frei
2021-02-12 20:30:51 +01:00
committed by GitHub
parent 5e9c7bc022
commit 7e4e2f3720
7 changed files with 58 additions and 13 deletions
+7 -3
View File
@@ -9,6 +9,7 @@ package versioner
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
@@ -64,12 +65,12 @@ func (v external) Archive(filePath string) error {
l.Debugln("archiving", filePath)
if v.command == "" {
return errors.New("Versioner: command is empty, please enter a valid command")
return errors.New("command is empty, please enter a valid command")
}
words, err := shellquote.Split(v.command)
if err != nil {
return errors.New("Versioner: command is invalid: " + err.Error())
return fmt.Errorf("command is invalid: %w", err)
}
context := map[string]string{
@@ -99,6 +100,9 @@ func (v external) Archive(filePath string) error {
combinedOutput, err := cmd.CombinedOutput()
l.Debugln("external command output:", string(combinedOutput))
if err != nil {
if eerr, ok := err.(*exec.ExitError); ok && len(eerr.Stderr) > 0 {
return fmt.Errorf("%v: %v", err, string(eerr.Stderr))
}
return err
}
@@ -106,7 +110,7 @@ func (v external) Archive(filePath string) error {
if _, err = v.filesystem.Lstat(filePath); fs.IsNotExist(err) {
return nil
}
return errors.New("Versioner: file was not removed by external script")
return errors.New("file was not removed by external script")
}
func (v external) GetVersions() (map[string][]FileVersion, error) {
+2 -2
View File
@@ -23,7 +23,7 @@ import (
)
var (
errDirectory = errors.New("cannot restore on top of a directory")
ErrDirectory = errors.New("cannot restore on top of a directory")
errNotFound = errors.New("version not found")
errFileAlreadyExists = errors.New("file already exists")
)
@@ -196,7 +196,7 @@ func restoreFile(method fs.CopyRangeMethod, src, dst fs.Filesystem, filePath str
if info, err := dst.Lstat(filePath); err == nil {
switch {
case info.IsDir():
return errDirectory
return ErrDirectory
case info.IsSymlink():
// Remove existing symlinks (as we don't want to archive them)
if err := dst.Remove(filePath); err != nil {
+33 -1
View File
@@ -47,5 +47,37 @@ func New(cfg config.FolderConfiguration) (Versioner, error) {
return nil, fmt.Errorf("requested versioning type %q does not exist", cfg.Type)
}
return fac(cfg), nil
return &versionerWithErrorContext{
Versioner: fac(cfg),
vtype: cfg.Versioning.Type,
}, nil
}
type versionerWithErrorContext struct {
Versioner
vtype string
}
func (v *versionerWithErrorContext) wrapError(err error, op string) error {
if err != nil {
return fmt.Errorf("%s versioner: %v: %w", v.vtype, op, err)
}
return nil
}
func (v *versionerWithErrorContext) Archive(filePath string) error {
return v.wrapError(v.Versioner.Archive(filePath), "archive")
}
func (v *versionerWithErrorContext) GetVersions() (map[string][]FileVersion, error) {
versions, err := v.Versioner.GetVersions()
return versions, v.wrapError(err, "get versions")
}
func (v *versionerWithErrorContext) Restore(filePath string, versionTime time.Time) error {
return v.wrapError(v.Versioner.Restore(filePath, versionTime), "restore")
}
func (v *versionerWithErrorContext) Clean(ctx context.Context) error {
return v.wrapError(v.Versioner.Clean(ctx), "clean")
}