2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-04 10:00:48 +00:00

Improve error reporting from restic diff

Instead of a stacktrace, restic diff 111 222 now reports:

	Fatal: no matching ID found for prefix "111"
This commit is contained in:
greatroar 2020-11-10 22:43:18 +01:00
parent 407843c5f9
commit 63e32c44c0
3 changed files with 24 additions and 17 deletions

View File

@ -55,9 +55,8 @@ func init() {
func loadSnapshot(ctx context.Context, repo *repository.Repository, desc string) (*restic.Snapshot, error) { func loadSnapshot(ctx context.Context, repo *repository.Repository, desc string) (*restic.Snapshot, error) {
id, err := restic.FindSnapshot(ctx, repo, desc) id, err := restic.FindSnapshot(ctx, repo, desc)
if err != nil { if err != nil {
return nil, err return nil, errors.Fatal(err.Error())
} }
return restic.LoadSnapshot(ctx, repo, id) return restic.LoadSnapshot(ctx, repo, id)
} }

View File

@ -2,17 +2,24 @@ package restic
import ( import (
"context" "context"
"fmt"
"github.com/restic/restic/internal/errors"
) )
// ErrNoIDPrefixFound is returned by Find() when no ID for the given prefix // A MultipleIDMatchesError is returned by Find() when multiple IDs with a
// could be found. // given prefix are found.
var ErrNoIDPrefixFound = errors.New("no matching ID found") type MultipleIDMatchesError struct{ prefix string }
// ErrMultipleIDMatches is returned by Find() when multiple IDs with the given func (e *MultipleIDMatchesError) Error() string {
// prefix are found. return fmt.Sprintf("multiple IDs with prefix %s found", e.prefix)
var ErrMultipleIDMatches = errors.New("multiple IDs with prefix found") }
// A NoIDByPrefixError is returned by Find() when no ID for a given prefix
// could be found.
type NoIDByPrefixError struct{ prefix string }
func (e *NoIDByPrefixError) Error() string {
return fmt.Sprintf("no matching ID found for prefix %q", e.prefix)
}
// Find loads the list of all files of type t and searches for names which // Find loads the list of all files of type t and searches for names which
// start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
@ -28,7 +35,7 @@ func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, er
if match == "" { if match == "" {
match = fi.Name match = fi.Name
} else { } else {
return ErrMultipleIDMatches return &MultipleIDMatchesError{prefix}
} }
} }
@ -43,7 +50,7 @@ func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, er
return match, nil return match, nil
} }
return "", ErrNoIDPrefixFound return "", &NoIDByPrefixError{prefix}
} }
const minPrefixLength = 8 const minPrefixLength = 8

View File

@ -2,6 +2,7 @@ package restic
import ( import (
"context" "context"
"strings"
"testing" "testing"
) )
@ -48,7 +49,7 @@ func TestFind(t *testing.T) {
} }
f, err = Find(context.TODO(), m, SnapshotFile, "NotAPrefix") f, err = Find(context.TODO(), m, SnapshotFile, "NotAPrefix")
if err != ErrNoIDPrefixFound { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), "NotAPrefix") {
t.Error("Expected no snapshots to be found.") t.Error("Expected no snapshots to be found.")
} }
if f != "" { if f != "" {
@ -58,8 +59,8 @@ func TestFind(t *testing.T) {
// Try to match with a prefix longer than any ID. // Try to match with a prefix longer than any ID.
extraLengthID := samples[0].String() + "f" extraLengthID := samples[0].String() + "f"
f, err = Find(context.TODO(), m, SnapshotFile, extraLengthID) f, err = Find(context.TODO(), m, SnapshotFile, extraLengthID)
if err != ErrNoIDPrefixFound { if _, ok := err.(*NoIDByPrefixError); !ok || !strings.Contains(err.Error(), extraLengthID) {
t.Error("Expected no snapshots to be matched.") t.Errorf("Wrong error %v for no snapshots matched", err)
} }
if f != "" { if f != "" {
t.Errorf("Find should not return a match on error.") t.Errorf("Find should not return a match on error.")
@ -67,8 +68,8 @@ func TestFind(t *testing.T) {
// Use a prefix that will match the prefix of multiple Ids in `samples`. // Use a prefix that will match the prefix of multiple Ids in `samples`.
f, err = Find(context.TODO(), m, SnapshotFile, "20bdc140") f, err = Find(context.TODO(), m, SnapshotFile, "20bdc140")
if err != ErrMultipleIDMatches { if _, ok := err.(*MultipleIDMatchesError); !ok {
t.Error("Expected multiple snapshots to be matched.") t.Errorf("Wrong error %v for multiple snapshots", err)
} }
if f != "" { if f != "" {
t.Errorf("Find should not return a match on error.") t.Errorf("Find should not return a match on error.")